发布文件输入通过AJAX后的FileReader二进制数据 [英] Posting File Input as FileReader Binary Data through AJAX Post

查看:503
本文介绍了发布文件输入通过AJAX后的FileReader二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过发布一个REST API上传到一个HTML文件输入到网页中的附件。 API文档指出,后是直二进制内容作为HTTP请求的身体,而不是一个表单文件上传。

I am trying to post an attachment uploaded to an HTML file input to a web page through a rest API. The API documentation states that the post is a straight binary content as the body of the HTTP request, not a form file upload.

我的code是如下:

$('#_testButton').bind('click', function () {
    var file = document.getElementById('_testFile').files[0]
    var reader = new FileReader();
    reader.onload = function () {
        $.ajax({
            url: '/attachmentURL',
            type: 'POST',
            data: reader.result
        })
    }
    reader.readAsBinaryString(file)
})

我需要这个工作了许多不同的MIME类型的,所以我没有在code声明它上面。不过,我已经试过宣布的contentType:应用程序/ MSWORD的.doc文件,并尝试过程数据:虚假的和的contentType:假

I need this to work for a number of different mimeTypes, so I didn't declare it in the code above. However, I have tried declaring contentType:'application/msword' for a .doc file and also tried processData:false and contentType:false.

中的数据被发布,它应该。然而,当我打开该文件,我得到一个消息,说MIMETYPE:应用程序/ x-空的空文件或用一串二进制字符的文件。我试过.doc文件和PDF文件,结果是相同的两个。

The data is being posted where it should. However, when I open the file, I get a message that says mimeType:application/x-empty with an empty file OR a file with a bunch of binary characters. I've tried .doc files and a pdf files and the result is the same for both.

有没有人有什么线索,我可以改变,使这项工作?

Does anyone have a clue what I can change to make this work?

推荐答案

简单地发送文件引用数据(与过程数据:假)做的工作对我来说至少有:

Simply sending the file reference as data (with processData: false) did the job for me at least:

$('#_testButton').bind('click', function () {
    var file = document.getElementById('_testFile').files[0];

    $.ajax({
        url: "/attachmentURL",
        type: "POST",
        data: file,
        processData: false
    });
});

据描述如下:<一href="https://developer.mozilla.org/en/DOM/XMLHtt$p$pquest/Sending_and_Receiving_Binary_Data#section_3">https://developer.mozilla.org/en/DOM/XMLHtt$p$pquest/Sending_and_Receiving_Binary_Data#section_3

发送一个字符串(即使该字符串重新presents二进制数据)将无法工作,因为浏览器将强行把它变成单向code和EN code为utf-8的specified 这将损坏二进制数据:

Sending a string (even if that string represents binary data) will not work because the browser will forcibly turn it into unicode and encode as utf-8 as specified which will corrupt the binary data:

如果数据是一个字符串,让编码为UTF-8。

If data is a string Let encoding be UTF-8.

让MIME类型是text / plain的;字符集= UTF-8

Let mime type be "text/plain;charset=UTF-8".

让请求实体体内可转化为统一code和EN codeD数据   为UTF-8。

Let the request entity body be data converted to Unicode and encoded as UTF-8.

发送文件引用( BLOB )将做到这一点:

Sending a file reference (blob) will do this:

如果数据的Blob如果对象的类型属性不是空   字符串让MIME类型是它的价值。

If data is a Blob If the object's type attribute is not the empty string let mime type be its value.

让请求实体正文是由数据psented原始数据再$ P $。

Let the request entity body be the raw data represented by data.

这篇关于发布文件输入通过AJAX后的FileReader二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆