使用 Dropbox API V2 上传文件到 Dropbox (JavaScript)
Dropbox.save() 方法已经被弃用,推荐使用 Dropbox API V2 中的 filesUpload 方法来实现文件上传。
以下是使用 Dropbox API V2 上传文件的示例代码:
-
首先,您需要在 Dropbox 开发者控制台中创建一个应用程序,并获取访问令牌。
-
安装 Dropbox JavaScript SDK。
<script src='https://unpkg.com/dropbox-sdk'></script>
- 初始化 Dropbox SDK,并创建一个 Dropbox 实例。
var dbx = new Dropbox.Dropbox({
accessToken: 'YOUR_ACCESS_TOKEN'
});
- 创建一个文件上传表单,并将选定的文件上传到 Dropbox。
<form>
<input type='file' id='file-input'>
<button type='submit' id='upload-button'>Upload</button>
</form>
var fileInput = document.getElementById('file-input');
var uploadButton = document.getElementById('upload-button');
uploadButton.addEventListener('click', function(event) {
event.preventDefault();
var file = fileInput.files[0];
dbx.filesUpload({
path: '/' + file.name,
contents: file
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
});
});
这将上传选定的文件到 Dropbox 根目录,并在控制台中打印响应对象。您可以使用其他路径来更改文件上传的位置。
原文地址: https://www.cveoy.top/t/topic/nct2 著作权归作者所有。请勿转载和采集!