简体   繁体   English

从API请求获取输出(客户端)

[英]Getting the output from an API request (client-side)

Note: I am inexperienced with APIs, JSON, REST, etc. 注意:我对API,JSON,REST等没有经验。

I am trying to implement FilePreviews into my site. 我正在尝试在我的网站中实现FilePreviews。 Its purpose is to take the url for any file type and convert it into a JPG or PNG. 其目的是获取任何文件类型的网址,并将其转换为JPG或PNG。

JavaScript 的JavaScript

var previews = new FilePreviews({
  debug: true,
  apiKey: 'MY_API_KEY'
});

var url = 'http://i.imgur.com/HQB8wtI.jpg';

var options = {
  size: {
    width: 100,
    height: 999
  },
  metadata: ['exif', 'ocr', 'psd'],
  format: 'jpg'
};

previews.generate(url, options);

The request is received by the developer's app. 该请求由开发者的应用程序接收。 The following results are shown in the site dashboard of the developer's app: 开发人员的应用程序的网站仪表板中显示以下结果:

{
    "preview": {
        "resized": true,
        "size": {
            "height": "178",
            "width": "100"
        },
        "page": 1,
        "url": "https://s3-us-west-2.amazonaws.com/[removed for privacy]/ec210ecf45d9d190539a241462c621f75adf2d4f835fb394a8d738d09fd412d6/HQB8wtI_100x999_1.jpg",
        "requested_size": "100x999",
        "original_size": {
            "height": "1024",
            "width": "576"
        }
    },
    "id": "25841aca-e176-4cf7-ac1d-b01ce604a765",
    "user_data": null,
    "status": "success",
    "url": "https://api.filepreviews.io/v2/previews/25841aca-e176-4cf7-ac1d-b01ce604a765/",
    "thumbnails": [
        {
            "resized": true,
            "size": {
                "height": "178",
                "width": "100"
            },
            "page": 1,
            "url": "https://s3-us-west-2.amazonaws.com/[removed for privacy]/ec210ecf45d9d190539a241462c621f75adf2d4f835fb394a8d738d09fd412d6/HQB8wtI_100x999_1.jpg",
            "requested_size": "100x999",
            "original_size": {
                "height": "1024",
                "width": "576"
            }
        }
    ],
    "original_file": {
        "metadata": {
            "ocr": null,
            "psd": null,
            "exif": null
        },
        "size": 82022,
        "extension": "jpg",
        "total_pages": 1,
        "encoding": "binary",
        "name": "HQB8wtI",
        "mimetype": "image/jpeg",
        "type": "image"
    }
}

My question is: How do I get the url for the file preview into my site? 我的问题是:如何将文件预览的URL进入我的网站? Each file is referenced dynamically and there will be many on a page for most areas of my site. 每个文件都是动态引用的,在我的网站的大部分区域,页面上都会有很多文件。 There seems to be no consistency in how FilePreviews is generating the folder on AWS S3, so I can't even use a clever PHP fix to solve it. FilePreviews在AWS S3上生成文件夹的方式似乎不一致,因此我什至不能使用巧妙的PHP修复程序来解决它。

Anyone care to assist and show me the ways of this programming world? 是否有人愿意协助并向我展示这个编程世界的方式?

I don't have experience with FilePreviews, but this is how I expect it should work. 我没有FilePreviews的经验,但这是我期望它能起作用的方式。

First, you add an image tag in your HTML, at the spot where you want the thumbnail to appear. 首先,在HTML中要显示缩略图的位置添加一个图像标签。 Give it an id, for example "thumb": 给它一个ID,例如“拇指”:

<img id="thumb"/>

Then, modify the last line of your script as follows: 然后,如下修改脚本的最后一行:

previews.generate(url, options, onPreviewReceived);

The third parameter is a callback function we must define elsewhere in the script. 第三个参数是我们必须在脚本的其他位置定义的回调函数。 I have called it onPreviewReceived, but you can choose your own name. 我将其称为onPreviewReceived,但是您可以选择自己的名称。 It is called when the result is received. 收到结果时调用它。 You can define the callback function as follows: 您可以如下定义回调函数:

function onPreviewReceived(err, result) {
    if (!err) {
        var thumbnailUrl = result.thumbnails[0].url;  // but see assumption
        document.getElementById("thumb").setAttribute("src", thumbnailUrl);
    }
}

This function assigns the url, obtained from the result, to the src attribute of the img tag. 此函数将从结果中获得的url分配给img标签的src属性。

Assumption : I expect the 'result' to be as described in your question. 假设 :我希望“结果”与您的问题中所述相同。 However, the client library documentation indicates that you will get a different result and that you should have a thumbnailUrl assignment like this: 但是, 客户端库文档指示您将获得不同的结果,并且应具有如下的thumbnailUrl分配:

var thumbnailUrl = result.previewUrl;

So please try that as well. 因此,请尝试一下。

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

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