如何使用imgur api将图像从本地磁盘上传到网络?

时间:2017-11-28 15:21:47

标签: javascript jquery imgur

我想构建一个文件上传器应用,允许用户将图片从本地磁盘上传到网络,我在网上搜索并找到此链接以使用imgur api this is url

但它没有用,我把代码放在jsfiddle.net但是也没有用this is the link of jsfiddle

这是代码:

<form id="imgur">
  <input type="file" class="imgur" accept="image/*" data-max-size="5000"/>
</form>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script> 
     $("document").ready(function() {

  $('input[type=file]').on("change", function() {

    var $files = $(this).get(0).files;

    if ($files.length) {

      // Reject big files
      if ($files[0].size > $(this).data("max-size") * 1024) {
        console.log("Please select a smaller file");
        return false;
      }

      // Begin file upload
      console.log("Uploading file to Imgur..");

      // Replace ctrlq with your own API key
      var apiUrl = 'https://api.imgur.com/3/image';
      var apiKey = 'ctrlq';

      var settings = {
        async: false,
        crossDomain: true,
        processData: false,
        contentType: false,
        type: 'POST',
        url: apiUrl,
        headers: {
          Authorization: 'Client-ID ' + apiKey,
          Accept: 'application/json'
        },
        mimeType: 'multipart/form-data'
      };

      var formData = new FormData();
      formData.append("image", $files[0]);
      settings.data = formData;

      // Response contains stringified JSON
      // Image URL available at response.data.link
      $.ajax(settings).done(function(response) {
        console.log(response);
      });

    }
  });
});
<script> 

所以身体有什么不对的帮助?

编辑:

我注册并获取关键client_id:5b9144f6bcc473e我把apiKey = 5b9144f6bcc473e但仍然没有任何帮助

0 个答案:

没有答案