如何在Firefox中使用带有二进制数据的xmlhttprequest,f.e。图片?

时间:2015-02-25 08:47:18

标签: javascript firefox binary xmlhttprequest

我目前正在关注

function readDataFromURL(fuFullHttpURL, fuCallMeOnLoad) {
    var MyThis = this;
    this.fullHttpURL = fuFullHttpURL;
    this.callMeOnLoad = fuCallMeOnLoad;
    var oReq = new XMLHttpRequest();
    oReq.open("GET", MyThis.fullHttpURL, true);
    oReq.responseType = "arraybuffer";
    oReq.onload = function(oEvent) {
        var blob = new Blob([oReq.response], {type: "image/jpg"});
        MyThis.callMeOnLoad(blob);
    };
    oReq.send();
}

但这只是为了下载。如何使用此代码上传? 当我尝试使用xmlhttprequest下载图像时,下载时存在大小限制。还有尺寸限制吗? 以前,每个浏览器都对这种尺寸限制的处理方式不同,所以我自己无法对此进行测试。

修改:https://stackoverflow.com/a/18120473/3716796似乎解释了上传。

1 个答案:

答案 0 :(得分:0)

您可以使用FormData在XMLHttpRequest中发送文件,如下所示。虽然Chrome& FF支持它很好,它只适用于IE10或更高版本。

  var xhr = new XMLHttpRequest();
  var file = document.getElementById("fileUpload");
  var formData = new FormData();
  formData.append("upload", file.files[0]);
  xhr.open("post", "your-url", true);
  xhr.setRequestHeader("Content-Type", "multipart/form-data");
  xhr.upload.onload = function(event){
    // handle successful upload
  };
  xhr.send(formData);