简体   繁体   English

通过原型Ajax调用提交二进制数据

[英]Submit binary data via Prototype Ajax call

I'm using Prototype to submit a POST request, and the postdata contains a number of fields, one of which is binary data from a file (in this case an Excel spreadsheet the user has selected for upload.) 我正在使用Prototype提交POST请求,并且postdata包含许多字段,其中之一是来自文件的二进制数据(在这种情况下,用户已选择要上传的Excel电子表格)。

I am using the HTML5 FileReader interface to get the contents of the file via FileReader.readAsBinaryString() which works well. 我正在使用HTML5 FileReader接口通过FileReader.readAsBinaryString()来获取文件的内容,效果很好。 If I use charCodeAt() to print various characters in the string then they come out with the expected values. 如果我使用charCodeAt()在字符串中打印各种字符,则它们将带有预期值。

However once I put this string data in an object (along with the other form fields) and pass it as the parameters option to Prototype's Ajax.Request() , the data arrives corrupted. 但是,一旦我将此字符串数据放入对象(以及其他表单字段)中,并将其作为parameters选项传递给Prototype的Ajax.Request() ,数据就会损坏。 Certain character values like 0x82 are replaced with 0xC2 0x82 , 0xAC is replaced with 0xC2 0xAC , and so on. 某些字符值(例如0x82被替换为0xC2 0x82 0xAC被替换为0xC2 0xAC ,依此类推。

I tried using window.atob() to base64 encode the string, but this fails with InvalidCharacterError: String contains an invalid character , so clearly there is some kind of processing going on which I need to avoid. 我尝试使用window.atob()对字符串进行base64编码,但这因InvalidCharacterError: String contains an invalid character而失败InvalidCharacterError: String contains an invalid character ,因此显然需要进行某种处理。

Does anyone know how to pass binary data through Prototype's Ajax.Request() while also including additional form fields in the same request? 有谁知道如何通过Prototype的Ajax.Request()传递二进制数据,同时在同一请求中还包含其他表单字段?

To do ajax file upload you should really use a FormData object 要上传ajax文件,您实际上应该使用FormData对象

var data = new FormData();
//var data = new FormData(someForm); if all your fields is in an html form
// add fields to form
data.append('fieldname',fieldvalue);
data.append('filefieldname',fileobject);
//etc

new Ajax.Request(url, {
    contentType: null,
    method: 'post',
    postBody: data,
    onSuccess: successFunction
})

With this method the server will see the request like if it was sent by a form with attribute enctype="multipart/form-data" 通过这种方法,服务器将看到请求,就像请求是由属性为enctype="multipart/form-data"

Doing this requires magic and pixie dust. 这样做需要魔术和小尘。 (well not really) (不是真的)

firstly you would need to put the form fields values as URL parameters instead of POST and then override the post body with the file contents. 首先,您需要将表单字段值作为URL参数而不是POST放置,然后用文件内容覆盖帖子正文。 For example 例如

new Ajax.Request('/url.php?rawupload=1&id='+$F('id')+'&label='+label,
    {
        'method':'post',
        'postBody':fileobject,
        'onSuccess':function() { alert('success'); }
    });

fileobject is the HTML5 file object retrieved from the file upload input element fileobject是从文件上传输入元素中检索到的HTML5文件对象

Yes its not the most elegant solution if you have lots of form fields. 是的,如果您有很多表单字段,它不是最优雅的解决方案。 You can also use Hash#toQueryString if you do have lots of form fields to build your query string instead of doing them by hand 如果您确实有很多表单字段来构建查询字符串,而不必手动进行操作,则也可以使用Hash#toQueryString

http://api.prototypejs.org/language/Hash/prototype/toQueryString/ http://api.prototypejs.org/language/Hash/prototype/toQueryString/

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

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