Sending binary data using multipart/form-data from a worker with IE11

百般思念 提交于 2019-12-13 16:46:48

问题


I'm trying to send a multipart/form-data from a worker with IE. I've already done it with Chrome, Firefox, Safari using formData objects (not supported IE, I need a manual one)

The binary data I'm sending is a crypto-js encrypted data. With formData objects I do:

var enc = new Buffer(encrypted.ciphertext.toString(CryptoJS.enc.Base64), 'base64');
formData.append("userFile" , new Blob([finalEncrypted], {type: 'application/octet-binary'}), 'encrypted')
this works fine generating a multipart like this(missed some parts of it):

request headers:

Accept:*/*
Accept-Encoding:gzip, deflate
Cache-Control:no-cache
Connection:keep-alive
Content-Length:30194
Content-Type:multipart/form-data; boundary=WebKitFormBoundary0.gjepwugw5cy58kt9

body:

--WebKitFormBoundary0.gjepwugw5cy58kt9
Content-Disposition: form-data; name="userFile"; filename="encrypted"
Content-Type: binary

all binary data

--WebKitFormBoundary0.cpe3c80eodgc766r--

With the manual multipart/form-data:

IE11 doesn't accept readAsBinaryString(deprecated)

I would like to avoid sending base64 encoded data(readAsDataURL)(33% payload)

The binary data I'm sending is a crypto-js encrypted data. I'm trying:

finalEncrypted = new Buffer(encrypted.ciphertext.toString(CryptoJS.enc.Base64), 'base64');

then in my manual multipart I tried to convert the buffer to a binary string:

item.toString('binary')

the multipart result looks looks this:

--WebKitFormBoundary642013568702052
Content-Disposition: form-data; name="userfile"; filename="encrypted"
Content-Type: binary

all binary data

ÐçÀôpRö3§]g7,UOÂmR¤¼ÚS"Ê÷UcíMÆÎÚà/,hy¼øsËÂú@WcGvºÆÞ²i¨¬Ç~÷®}éá?'é·J]þ3«áEÁÞ,4üBçðºÇª bUÈú4
T\Ãõ=òEnýR  _[1J\O-ïǹ C¨\Ûøü^%éÓÁóJNÓï¹LsXâx>\aÁV×Þ^÷·{|­'

On the .NET server we check the hash calculated on client versus calculated on server. Server reply that hashes doesn't match. This makes me think that I'm not sending the file correctly.


回答1:


It looks like you did not yet get a solution, at least you did not post it here if you had one.

On my end I use jQuery which handles the low level nitty gritty of the actual post.

It may be that you are doing one small thing wrong and IE fails on it. Since you do not show what you used with FormData. It is rather difficult to see whether you had a mistake in there.

// step 1. setup POST data
var data = new FormData();
data.append("some_variable_name", "value_for_that_variable");
data.append("some_blob_var_name", my_blob);
data.append("some_file_var_name", my_file);

// step 2. options
var ajax_options =
    {
        method: "POST",
        processData: false,
        data: data,
        contentType: false,
        error: function(jqxhr, result_status, error_msg)
        {
            // react on errors
        },
        success: function(data, result_status, jqxhr)
        {
            // react on success
        },
        complete: function(jqxhr, result_status)
        {
            // react on completion (after error/success callbacks)
        },
        dataType: "xml" // server is expected to return XML only
    };

// step 3. send
jQuery.ajax(uri, ajax_options);

Step 1.

Create a FormData object and fills the form data, that includes variables and files. You may even add blobs (JavaScript objects, will be transformed to JSON if I'm correct.)

Step 2.

Create an ajax_options object to your liking. Although here I show your the method, processData, data, contentType as they must be in case you want to send a FormData. At least, that works for me... It may be possible to change some of those values.

The dataType should be set to whatever type you expect in return.

Step 3.

Send the request using the ajax() function from the jQuery library. It will build the proper header and results as required for the client's browser.



来源:https://stackoverflow.com/questions/27921339/sending-binary-data-using-multipart-form-data-from-a-worker-with-ie11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!