jQuery 文件上传在 IE 8 和 9 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15425934/
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
jQuery fileupload not working in IE 8 and 9
提问by segFault
This code works in FF and chrome. In IE 8 or 9 I get a 500 error saying a not null property is null.
此代码适用于 FF 和 chrome。在 IE 8 或 9 中,我收到 500 错误,指出非空属性为空。
Here is the html
这是html
<div id="upload_button_div_general" class="fileupload-buttonbar" data-url="/Upload/SomeMethod">
<label class="fileinput-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button">
<span class="ui-button-text">
<span>Add Documents...</span>
</span>
<input id="upload_button" type="file" name="postedFiles" multiple="" />
</label>
</div>
<div id="UploadMessage" data-bind="visible: showMessage">
<span>Documents</span>
<ul data-bind="foreach: upload()">
<li>
<a href="#" data-bind="click: $parent.openFile">
<span data-bind="text: $data.fileName"> </span>
</a>
</li>
</ul>
</div>
Here is the javascript
这是javascript
function Upload(div, additionalParams, successFunc, failureFunc) {
$('#' + div).fileupload({
dataType: 'json',
url: rootPath + 'Upload/SomeMethod',
formData: additionalParams,
start: function (e, data) {
showLoading();
},
stop: function (e, data) {
hideLoading();
},
add: function (e, data) {
data.submit();
},
always: function (e, data) {
var result = data.result;
if (result.HasError) {
failureFunc(result.Error);
} else {
successFunc(result);
}
}
});
};
The controller method is
控制器方法是
public virtual JsonResult SomeMethod(IEnumerable<HttpPostedFileBase> postedFiles, int id)
采纳答案by segFault
I was able to get it working by including jquery.iframe-transport.js and then I had to remove my knockout "with" data-bind from the div to get it to work in IE8 because it worked in IE9. (I had a with binding above my posted code) Thanks for all the suggestions.
我能够通过包含 jquery.iframe-transport.js 来使其工作,然后我不得不从 div 中删除我的淘汰赛“with”数据绑定以使其在 IE8 中工作,因为它在 IE9 中工作。(我在我发布的代码上方有一个绑定)感谢所有建议。
回答by Mr_Green
As this function is working fine in FF, there is only one possibility that the variables you are passing here are undefined just for IE.
由于此函数在 FF 中运行良好,因此只有一种可能性是您在此处传递的变量未定义仅适用于 IE。
Check each variables values in IE console.
检查 IE 控制台中的每个变量值。
Hint: IE is strict about types and everything.
提示:IE 对类型和一切都有严格的要求。
For example:
例如:
parseInt(Number);
FF and Chrome assumes it as decimal value whereas IE assumes it as octal number.
So, giving parseInt(Number,10)
is recommended.
FF 和 Chrome 将其假定为十进制值,而 IE 将其假定为八进制数。所以,parseInt(Number,10)
建议给予。
And even regarding dates, if you provide
甚至关于日期,如果你提供
var currentDate = new Date("March 18, 2013 11:13:00")
Works good in Chrome and FF, but shows undefined
or invalid Date
in IE.
在 Chrome 和 FF 中运行良好,但在 IE 中显示undefined
或显示invalid Date
。
You can find more about the recommended notation of the date here
您可以在此处找到有关推荐的日期表示法的更多信息
So, in the above examples I am just trying to say you that you may have forgotten to declare type, or correct notation.
因此,在上面的示例中,我只是想告诉您,您可能忘记声明type或正确的符号。
Though, this is not the answer you were looking for, I hope this information will help you.
虽然,这不是您要寻找的答案,但我希望这些信息对您有所帮助。
Update: As the error is 500 error
, then the problem could be more possibly in rootPath
variable's value.
更新:由于错误是500 error
,那么问题可能更可能出在rootPath
变量的值上。
回答by Cosmin B
Just set the content-type to text/html and send it as JSON.
只需将内容类型设置为 text/html 并将其作为 JSON 发送。
This should work for all browsers.
这应该适用于所有浏览器。
回答by Bhavin Mistry
$("#txt1").fileupload({
replaceFileInput: false,
dataType: "json",
datatype:"json",
url: "<%=Page.ResolveUrl("~/WebService/AddAttachment.ashx")%>",
done: function (e, data) {
$.each(data.result, function (index, value) {
//You get the response data in here from your web service
})
$("#txt1").val("");
}`enter code here`
});
This is tested and working fine in both IE8 and IE9 + above. Please make sure to use the correct dataType:"json" (or datatype:"json") and also make sure your response of web service method is correct. Thanks
这在 IE8 和 IE9 + 上面都经过测试并且工作正常。请确保使用正确的数据类型:“json”(或数据类型:“json”),并确保您对 Web 服务方法的响应是正确的。谢谢
回答by user3062464
On IE if you return data like json when you upload a file you can get this data(json) like this:
在 IE 上,如果您在上传文件时返回类似 json 的数据,您可以像这样获取此数据(json):
done: function (e, datos)
{
try
{
//This in FF, Chrome, Safari
data=eval(JSON.parse(datos.result));
}catch (er)
{
//This in IE
data=eval(JSON.parse(datos.result[0].documentElement.innerText));
}
回答by Carlos Castillo
If by jQuery fileupload you mean using http://blueimp.github.com/jQuery-File-Upload/then it clearly says in the browser support section that IE 10+ is required for every functionality they implemented.
如果 jQuery fileupload 是指使用http://blueimp.github.com/jQuery-File-Upload/那么它在浏览器支持部分清楚地说明他们实现的每个功能都需要 IE 10+。
https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support
https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support
If you meant another plugin, just disregard my comment.
如果您的意思是另一个插件,请忽略我的评论。