我正在尝试使用JS SDK在Dropbox上上传一个文件。下面是我试图调用函数的html代码:
<!DOCTYPE html>
<html>
<head>
<script src="get_from_cin7.js"></script>
<meta charset="utf-8" />
<title>Dropbox JavaScript SDK</title>
<!--<link rel="stylesheet" href="/styles.css">-->
</head>
<body>
<form onSubmit="Dropupload()">
<input type="file" id="file-upload" />
<button type="submit">Submit</button>
</form>
</body>
</html>
这是定义函数的文件
import { Dropbox } from 'dropbox';
const dbx = new Dropbox({
accessToken: '<ACCESS TOKEN>', //I replaced it with my access token in the code
fetch
});
function Dropupload() {
var file = fileIput.files[0];
dbx.filesUpload({path: '/' + file.name, contents: file})
.then(function(response) {
var results = document.getElementById('results');
results.appendChild(document.createTextNode('File uploaded!'));
document.write('MISSION COMPLETE');
})
.catch(function(error) {
console.error(error);
document.write('BETTER LUCK NEXT TIME');
});
}
但是,由于我不知道的原因,我的函数不能被调用。我得到错误“referenceerror:Dropupload is not defined”,我不知道这是否与问题有关,但我得到另一个错误:“syntaxerror:import declarations may only at top level of a module”。
我只是要测试上传,所以这是现在的全部代码。
这里到底出了什么问题?
您忘记了script
标记中的type=“module”
。如果不这样做,ES6模块语法将无法工作。
您还需要将dropupload
附加到window
,否则它是模块本地的。
window.Dropupload = function() {
var file = fileIput.files[0];
...