上传图像二进制文件 - 使用imageshack api

时间:2014-02-26 09:04:55

标签: javascript firefox-addon image-uploading imageshack

将主题从谷歌群组转移到此处,以便它可以帮助正在询问的人。

imageshack api:http://api.imageshack.us/

最终的http reqeust正在返回json:

{"success":true,"process_time":325,"result":{"max_filesize":5242880,"space_limit":52428800,"space_used":0,"space_left":52428800,"passed":0,"failed":0,"total":0,"images":[]}}

这不好,因为它没有上传:(

它应该返回一个图像对象。 http://api.imageshack.us/#h.ws82a1l6pp9g 因为这是imageshack api上的上传图片部分所说的

请帮助:(

我的扩展程序代码

var blobUrl;
var makeBlob = function () {
        bigcanvas.toBlob(function (blob) {
                var reader = new window.FileReader();
                reader.readAsBinaryString(blob);
                reader.onloadend = function () {
                        blobBinaryString = reader.result;
                        blobUrl = blobBinaryString;
                        Cu.reportError(blobUrl);
                        uploadBlob();
                }
        });
};

var uploadedImageUrl;
var uploadBlob = function () {
        HTTP('POST', 'https://api.imageshack.us/v1/images', {
                contentType: 'application/x-www-form-urlencoded',
                //'album=' + urlencode('Stock History') + '&
                body: 'auth_token=' + urlencode(auth_token) + 'file@=' + blobUrl,
                onSuccess: function (status, responseXML, responseText, headers, statusText) {
                        Cu.reportError('XMLHttpRequest SUCCESS - imageshack uploadBlob\n' + statusText + '\n' + responseText);
                        eval('var json = ' + responseText);
                        uploadedImageUrl = json.direct_link;
                        submitBamdex();
                },
                onFailure: function (status, responseXML, responseText, headers, statusText) {
                        Cu.reportError('XMLHttpRequest FAILLLLLLLL - imageshack uploadBlob\n' + statusText + '\n' + responseText);
                }
        });
};


makeBlob(); //callllll the func

2 个答案:

答案 0 :(得分:1)

此代码将画布上的绘图上传到ima​​geshack

可以复制粘贴,但必须更新一些内容:

  • 更新用户名
  • 更新密码
  • 从画布上传ID为“bigcanvas”
  • 的画布
  • 更新您的API密钥

...

//this code uploads a drawing on a canvas to imageshack
var auth_token;
var loginImageshack = function() {
    HTTP('POST','https://api.imageshack.us/v1/user/login',{
        contentType: 'application/x-www-form-urlencoded',
        body: 'user=USERNAME_TO_IMAGESHACK_HERE&password=' + urlencode('PASSWORD_TO_USERNAME_FOR_IMAGESHACK_HERE'),
        onSuccess: function(status, responseXML, responseText, headers, statusText) {
            Cu.reportError('XMLHttpRequest SUCCESS - imageshack login\n' + statusText + '\n' + responseText);
            eval('var json = ' + responseText);
            auth_token = json.result.auth_token;
            makeImageshackFile();
        },
        onFailure: function(status, responseXML, responseText, headers, statusText) {
            Cu.reportError('XMLHttpRequest FAILLLLLLLL - imageshack login\n' + statusText + '\n' + responseText);
        }
    });
};

var uploadedImageUrl;
var makeImageshackFile = function() {
    var fd = new window.FormData();
    fd.append("api_key", 'A835WS6Bww584g3568efa2z9823uua5ceh0h6325'); //USE YOUR API KEY HERE
    fd.append("auth_token", auth_token);
    fd.append('album', 'Stock History');
    fd.append('title', 'THE-title-you-want-showing-on-imageshack')
    fd.append("file@", bigcanvas.mozGetAsFile("foo.png")); //bigcanvas is a canvas with the image drawn on it: var bigcanvas = document.querySelector('#bigcanvas');
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        switch (xhr.readyState) {
                case 4:
                    if (xhr.status==0 || (xhr.status>=200 && xhr.status<300)) {
                         Cu.reportError('XHR SUCCESS - \n' + xhr.responseText);
                        eval('var json = ' + xhr.responseText);
                        //ensure it passed else redo it I didnt program in the redo thing yet
                        //succesful json == {"success":true,"process_time":1274,"result":{"max_filesize":5242880,"space_limit":52428800,"space_used":270802,"space_left":52157998,"passed":1,"failed":0,"total":1,"images":[{"id":1067955963,"server":703,"bucket":2397,"lp_hash":"jj9g5p","filename":"9g5.png","original_filename":"foo.png","direct_link":"imageshack.us\/a\/img703\/2397\/9g5.png","title":"082813 200AM PST","description":null,"tags":[""],"likes":0,"liked":false,"views":0,"comments_count":0,"comments_disabled":false,"filter":0,"filesize":1029,"creation_date":1377681549,"width":760,"height":1110,"public":true,"is_owner":true,"owner":{"username":"bamdex","avatar":{"server":0,"filename":null}},"next_images":[],"prev_images":[{"server":59,"filename":"06mm.png"},{"server":706,"filename":"a1fg.png"}],"related_images":[{"server":59,"filename":"06mm.png"},{"server":41,"filename":"xn9q.png"},{"server":22,"filename":"t20a.png"},{"server":547,"filename":"fipx.png"},{"server":10,"filename":"dg6b.png"},{"se
                        uploadedImageUrl = json.result.images[0].direct_link;
                        Cu.reportError('succesfully uploaded image');
                    } else {
                        Cu.reportError('XHR FAIL - \n' + xhr.responseText);
                    }
                    break;
            default:
                //blah
        }
    }
    xhr.open("POST", "https://api.imageshack.us/v1/images");
    xhr.send(fd);
}

loginImageshack();

上面代码的重要说明

    如果要将插件提交到AMO
  • 应使用JSON.parse而不是eval
  • 也可能会从使用窗口更改为Services.appShel.hiddenDOMWindow,因此new window.FormData();会变为new Services.appShel.hiddenDOMWindow.FormData();var formData = Components.classes["@mozilla.org/files/formdata;1"].createInstance(Components.interfaces.nsIDOMFormData);Cu.import('resource://gre/modules/FormData.jsm')

上述代码所需的辅助函数:

const {classes: Cc, interfaces: Ci, utils: Cu, Components: components} = Components
Cu.import('resource://gre/modules/Services.jsm');

...

function urlencode(str) {
    return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
};

...

//http request
const XMLHttpRequest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"];

/** 
 * The following keys can be sent:
 * onSuccess (required)  a function called when the response is 2xx
 * onFailure             a function called when the response is not 2xx
 * username              The username for basic auth
 * password              The password for basic auth
 * overrideMimeType      The mime type to use for non-XML response mime types
 * timeout               A timeout value in milliseconds for the response
 * onTimeout             A function to call if the request times out.
 * body                  A string containing the entity body of the request
 * contentType           The content type of the entity body of the request
 * headers               A hash of optional headers
 */
function HTTP(method,url,options)
{
   var requester = new XMLHttpRequest();

   var timeout = null;
   if (!options.synchronizedRequest) {

      requester.onreadystatechange = function() {
         switch (requester.readyState) {
            case 0:
               if (options.onUnsent) {
                  options.onUnsent(requester);
               }
            break;
            case 1:
               if (options.onOpened) {
                  options.onOpened(requester);
               }
            break;
            case 2:
               if (options.onHeaders) {
                  options.onHeaders(requester);
               }
            break;
            case 3:
               if (options.onLoading) {
                  options.onLoading(requester);
               }
            break;
            case 4:
               if (timeout) {
                  clearTimeout(timeout);
               }
               if (requester.status==0 || (requester.status>=200 && requester.status<300)) {
                  options.onSuccess(
                     requester.status,
                     requester.responseXML,
                     requester.responseText,
                     options.returnHeaders ? _HTTP_parseHeaders(requester.getAllResponseHeaders()) : null,
                     requester.statusText
                  );
               } else {
                  if (options.onFailure) {
                     options.onFailure(
                        requester.status,
                        requester.responseXML,
                        requester.responseText,
                        options.returnHeaders ? _HTTP_parseHeaders(requester.getAllResponseHeaders()) : null,
                        requester.statusText
                     );
                  }
               }
            break;
         }
      }
   }

   if (options.overrideMimeType) {
      requester.overrideMimeType(options.overrideMimeType);
   }
   if (options.username) {
      requester.open(method,url,!options.synchronizedRequest,options.username,options.password);
   } else {
      requester.open(method,url,!options.synchronizedRequest);
   }
   if (options.timeout && !options.synchronizedRequest) {
      timeout = setTimeout(
          function() {
             var callback = options.onTimeout ? options.onTimeout : options.onFailure;
             callback(0,"Operation timeout.");
          },
          options.timeout
      );
   }
   if (options.headers) {
      for (var name in options.headers) {
         requester.setRequestHeader(name,options.headers[name]);
      }
   }
   if (options.sendAsBinary) {
        Cu.reportError('sending as binary');
       requester.sendAsBinary(options.body);
   } else if (options.body) {
      requester.setRequestHeader("Content-Type",options.contentType);
      requester.send(options.body);
   } else {
      requester.send(null);
   }
   if (options.synchronizedRequest) {
      if (requester.status==0 || (requester.status>=200 && requester.status<300)) {
         options.onSuccess(
            requester.status,
            requester.responseXML,
            requester.responseText,
            options.returnHeaders ? _HTTP_parseHeaders(requester.getAllResponseHeaders()) : null,
            requester.statusText
         );
      } else {
         if (options.onFailure) {
            options.onFailure(
               requester.status,
               requester.responseXML,
               requester.responseText,
               options.returnHeaders ? _HTTP_parseHeaders(requester.getAllResponseHeaders()) : null,
               requester.statusText
            );
         }
      }
      return {
         abort: function() {
         }
      };
   } else {
      return {
         abort: function() {
            clearTimeout(timeout);
            requester.abort();
         }
      };
   }
}
function _HTTP_parseHeaders(headerText)
{
   var headers = {};
   if (headerText) {
      var eol = headerText.indexOf("\n");
      while (eol>=0) {
         var line = headerText.substring(0,eol);
         headerText = headerText.substring(eol+1);
         while (headerText.length>0 && !headerText.match(_HTTP_HEADER_NAME)) {
            eol = headerText.indexOf("\n");
            var nextLine = eol<0 ? headerText : headerText.substring(0,eol);
            line = line+' '+nextLine;
            headerText = eol<0 ? "" : headerText.substring(eol+1);
         }
         // Parse the name value pair
         var colon = line.indexOf(':');
         var name = line.substring(0,colon);
         var value = line.substring(colon+1);
         headers[name] = value;
         eol = headerText.indexOf("\n");
      }
      if (headerText.length>0) {
         var colon = headerText.indexOf(':');
         var name = headerText.substring(0,colon);
         var value = headerText.substring(colon+1);
         headers[name] = value;
      }
   }
   return headers;
}

答案 1 :(得分:1)

对我有用的是阅读有关

所以在我的react组件onChange处理程序中,我使用新的FileReader读取event.target.files [0],readAsDataURL(file),并将Base64编码的字符串设置为state。

我有条件地渲染img src = {base64stringfromState}以提供正确图像的确认,然后在onSubmit上,将此“数据URI”(Base64字符串)转换为带有在某处找到的两个代码之一的blob(didn最终不会使用第一个,但这很有用,并且花了很长时间才找到):

const dataURItoBlob = (dataURI) => {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
        byteString = atob(dataURI.split(',')[1]);
    else
        byteString = unescape(dataURI.split(',')[1]);
    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
    // write the bytes of the string to a typed array
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ia], {type:mimeString});
}

const blob = await fetch(base64imageString).then(res => res.blob())

在构建照片上传/请求的过程中,我们无需执行所有操作即可获取图像的新版本或其他内容,然后在其中将其斑点化:

event.preventDefault()
const blob = await fetch(base64stringfromState).then(res => res.blob())
const formData = new FormData()
formData.append('file@', blob)
formData.append('api_key', 'XXXXX')
formData.append('auth_token', 'XXXXXXXXXX')
formData.append('album', 'youralbumname')
const res = await axios.post('https://api.imageshack.com/v2/images', formData, {headers{'Content-Type':'multipart/form-data'}})

然后,我们要存储上传的图像,只需将https://附加并记录返回的直接链接以供其ID旁边存储,以便以后需要时可以将其删除。根据之前的代码,他们吐出

res.data.result.images[0].direct_link
res.data.result.images[0].id

这是个解决难题,希望可以帮助其他人将照片上传到ima​​geshack api cuz,考虑到竞争对手的限制,这可能是一个很大的价值。