javascript enctype = "multipart/form-data" 在 ie9 和 chrome 之间的工作方式不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6278750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:08:48  来源:igfitidea点击:

enctype = "multipart/form-data" works different between ie9 and chrome

javascriptformscross-domainmultipartform-data

提问by pvd

I want to post some data to another domain with the enctype equal to multipart/form-data, because I want the unmodified data in the server side. Here is my test code:

我想将一些数据发布到另一个域,enctype 等于 multipart/form-data,因为我想要服务器端的未修改数据。这是我的测试代码:

<textarea name="txt" rows="20" cols="80">
</textarea>
<script>
function x_domain_post(url, data)
{
    var dd = document.createElement('div');
    var ifrname = "client_proxy";
    dd.innerHTML = "<iframe id='" + ifrname + "' name='" + ifrname + "' width=0 height=0 ></iframe>";
    document.getElementsByTagName('body')[0].appendChild(dd);
    var ifr = document.getElementById(ifrname);

    var f = document.createElement('form');
    f.action = url;
    f.method = "post";
    f.enctype = "multipart/form-data";
    f.target = ifr.name;
    f.innerHTML = f.innerHTML + "<textarea name='request'>default</textarea>";
    document.getElementsByTagName("body")[0].appendChild(f);
    document.getElementsByName("request")[0].value=data;
    f.submit()
    setTimeout(function(){document.getElementsByTagName("body")[0].removeChild(f);}, 1000);
}

</script>
<button onclick="x_domain_post('http://192.168.232.128/add', document.getElementsByName('txt')[0].value)">

The request in chrome is:

chrome 中的请求是:

...
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary9zCD31eJSHkdb8ul
...
------WebKitFormBoundary9zCD31eJSHkdb8ul
Content-Disposition: form-data; name="request"

a

b

o
------WebKitFormBoundary9zCD31eJSHkdb8ul--

But in IE9:

但是在 IE9 中:

POST /add HTTP/1.1
...
Content-Type: application/x-www-form-urlencoded
...
request=a%0D%0A%0D%0Ab%0D%0A%0D%0Ao

Any of your help will be appreciated!

您的任何帮助将不胜感激!

回答by Matthew Wilson

Does this help?

这有帮助吗?

http://www.bennadel.com/blog/1273-Setting-Form-EncType-Dynamically-To-Multipart-Form-Data-In-IE-Internet-Explorer-.htm

http://www.bennadel.com/blog/1273-Setting-Form-EncType-Dynamically-To-Multipart-Form-Data-In-IE-Internet-Explorer-.htm

Apparently in IE, you have to set the "encoding" of the form rather than the "enctype". The good news is, you can set both values without concern and this will take care of the problem

显然在 IE 中,您必须设置表单的“编码”而不是“enctype”。好消息是,您可以毫无顾虑地设置这两个值,这将解决问题