Can Dropbox Saver accept data from createObjectURL()?

半世苍凉 提交于 2019-12-09 22:32:35

问题


The Dropbox Chooser and Saver tools seem very cool for these two tasks:

  • Let the user pick a file from their Dropbox, and the page can download it (i.e., Dropbox-to-client).
  • Let the user choose a destination in their Dropbox to which to save a file sitting at some URL on a server (i.e., server-to-Dropbox).

That's asymmetrical. This StackOverflow question asks if it's possible to send a file to Dropbox Saver directly from the client. (This is especially handy if one is writing a client-side-only app, wanting Dropbox to stand in for the server.)

A comment in one answer says that the Core API or Sync API can do so. But it would be a shame to introduce those more complex APIs if not needed; Chooser and Saver are delightfully easy-to-use. Furthermore, it's not clear to me whether the UI provided by the Saver would still be available in that case.

My question: What's the easiest way to save a file from the client (e.g., from data in a createObjectURL URL) into the user's Dropbox, still using the Saver UI if possible? (By "easiest" I mean brief, easy-to-read-and-maintain code.) Specific JavaScript code would be ideal.


回答1:


As in James Foster's comment above, Dropbox now accepts data URIs. Consequently, if one has the data in the form of a data URI, one can call Dropbox.save(dataURI,filename,options) as documented here.

But there is a slight problem: To create a data URI the usual way (with FileReader) requires an asynchronous call to readAsDataURL. But Dropbox.save() can only be called in response to a user interaction (such as a click). So in a click handler, if you must first create the data URI asynchronously, then by the time you get to the callback, the Dropbox.save() function can no longer be called.

In my case, it was possible to create the data URI without the FileReader API, since I was only storing HTML data. One can simply write "data:text/html,"+encodeURIComponent(myHTMLData) to create the data URI, as documented here. For non-text data that solution will not work, and you'd have to try something more complicated, perhaps like what's documented here.



来源:https://stackoverflow.com/questions/26457316/can-dropbox-saver-accept-data-from-createobjecturl

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