问题
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?
Also, won't these methods be memory intensive and make the app/device slow, especially when uploading big files like 150mb or so?
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:
- When uploading files, you should supply the data in the
contents
parameter, e.g., on the filesUpload method. That should currently support eitherstring
orBuffer
.
There's an example of that in node here.
- 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.
- 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