How to upload a file to Dropbox using JS SDK?

試著忘記壹切 提交于 2019-12-11 22:56:05

问题


  1. I am not sure whether to read the file as as Array Buffer or as a dataURI or as a binary stream. Which would be better?

  2. Also, won't these methods be memory intensive and make the app/device slow, especially when uploading big files like 150mb or so?

  3. And yet another thing is whether the file will be "openable" as it was originally? Example: If I read a .zip file using one of the methods and then download the file later, will it still open as ZIP?

I am partly confused because of this analogy: We can easily view a JPG or PNG file and we can also display them in a browser using their dataURI (base-64)... but when we save that base-64 string as a JPG or PNG file, the image cannot be seen. Therefore, I cannot understand how should I read the contents of the file to be able to upload it on dropbox (keeping in mind that a file can be around 150MB too) and still retain how the file originally was.

Let's say I want to upload an .mp3 or a .zip file. Then how should I upload this to a user's dropbox account using the Javascript Dropbox SDK?

Kindly clear this confusion and help me out!

Thanks!


回答1:


When using the Dropbox API v2 JavaScript SDK:

  1. When uploading files, you should supply the data in the contents parameter, e.g., on the filesUpload method. That should currently support either string or Buffer.

There's an example of that in node here.

  1. For large files like this, you should instead use upload sessions, which allows you to upload files in smaller pieces:
    • filesUploadSessionStart
    • filesUploadSessionAppendV2
    • filesUploadSessionFinish

There's an example of using these here. You can choose how big to make each piece.

  1. The file you upload to Dropbox will contain exactly the data you supply in the upload call. So, if you upload a zip file, and then download it again later, it will still be in the same format, and in the case of a zip file, that means you will be able to unzip it.

Note that you should not apply any extra encoding to the file data when uploading it. For example, when displaying an image in a browser, you may base64 encode the data to make it usable in a data URI, but you should not do so when uploading the file for storage.



来源:https://stackoverflow.com/questions/56575928/how-to-upload-a-file-to-dropbox-using-js-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!