简体   繁体   English

使用JavaScript API从Dropbox显示图像

[英]Display images from dropbox with javascript api

I'm trying to create a page that could show all pictures in a dropbox folder through the Javascript API. 我正在尝试创建一个页面,该页面可以通过Javascript API在Dropbox文件夹中显示所有图片。

I was able to set up my dropbox app properly, and I am able to get a list of files. 我能够正确设置我的保管箱应用程序,并且能够获取文件列表。

I'm stuck at the part to get a URL I could actually use to show the picture in HTML. 我被困在获取URL的地方,实际上我可以用它来显示HTML中的图片。 I tried the following code, to try and get the URL for 1 image for the time being: 我尝试了以下代码,以尝试暂时获取1张图片的网址:

dbx.filesListFolder({path: ""})
    .then(function(response) {
        console.log(response);
        // ↑ this works!
        dbx.filesGetThumbnail({path: response.entries[0].path_display, format: "jpeg", size: "w64h64"})
            .then(function(result) {
                window.data = result;
                console.log(result);
            })
        // closures and rest of code...

Inspecting the window.data or the console.log(result) , I cannot seem to find any URL I could use in my HTML. 检查window.dataconsole.log(result) ,我似乎找不到可以在HTML中使用的任何URL。

Any pointers to head me in the right direction? 有任何指向我正确方向的指示吗? I'm still new to the Dropbox Javascript API. 我还是Dropbox Javascript API的新手。

Kudos to Greg 格雷格的荣誉

The filesGetThumbnail method doesn't itself return a URL for the thumbnail data. filesGetThumbnail方法本身不会返回缩略图数据的URL。 It returns the raw thumbnail data directly. 它直接返回原始缩略图数据。 If you want a URL to display locally in the browser, you may want to something like this: 如果您希望URL在浏览器中本地显示,则可能需要执行以下操作:

dbx.filesGetThumbnail({"path": "/test.jpg"})
  .then(function(response) {
    var img = document.createElement('img');
    img.src=window.URL.createObjectURL(response.fileBlob);
    document.body.appendChild(img);
  })
  .catch(function(error) {
    console.log("got error:");
    console.log(error);
  });

BTW, you can find all of the API v2 JavaScript SDK methods documented here . 顺便说一句,您可以在此处找到所有API v2 JavaScript SDK方法。

For others has the same issue :) Now Dropbox JS Api returns base64 image data instead, so you need to do something like this: 对于其他人也有相同的问题:)现在,Dropbox JS Api代替返回base64图像数据,因此您需要执行以下操作:

  var img = document.createElement('img');
  img.src = "data:image/jpg;base64, " + <data returned>;

data:image/jpg depends on which image type you requested data:image/jpg取决于您请求的图像类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM