gpt4 book ai didi

javascript - IE不支持动态上传文件?

转载 作者:行者123 更新时间:2023-11-30 05:50:58 26 4
gpt4 key购买 nike

如果我像这样在 HTML 中显式写出一个表单:

 <form action='upload_1-img.php' enctype='multipart/form-data' method='post'>
<input type='file' id='image' name='image'><input type='submit'>
</form>

然后在 IE 中一切正常。但是,如果我执行以下操作,它在 Chrome 和 FF 中有效,但在 IE8 中无效:

<html>
<head>
<script>
$(document).ready(function(){

imgform = document.createElement('form');
imgform.id = 'imgform';
imgform.method='post';
imgform.enctype='multipart/form-data';
imgform.action ='upload_1-img.php';
$('body').append(imgform);
$('#imgform').append("<input type='file' id='image' name='image' /><input type=submit>");
});
</script>
</head>
<body>
</body>
</html>

在这种情况下,如果我在 upload_1-img.php 中使用 var_dump($_FILES),它会返回一个空数组。当同样的表单明确编码为 HTML 时,IE8 可以正常上传文件。但是在javascript中动态创建表单后,我需要文件上传才能工作。在 IE8 中进行这项工作的解决方法是什么?

最佳答案

尝试做这样的事情:

var form=document.createElement("<form id='imgform' action='upload_1-img.php' enctype='multipart/form-data' method='post'>");
$('body').append(form);
$('#imgform').append("<input type='file' id='image' name='image' /><input type=submit>");

来源:http://verens.com/2005/07/06/ie-bugs-dynamically-creating-form-elements/

如果您查看文章的底部,它指出

The above code will mostly work in IE, except when you actually need to use the multipart aspect of it (uploading a file, for example), when it will barf.

In this case, IE seems to insist on the following crappy code:

form=document.createElement('<form action="shoppingcart_xhr.php" method="POST" > enctype="multipart/form-data" target="shoppingcart_iframe">');

关于javascript - IE不支持动态上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com