javascript 通过javascript提交表单时IE中的访问被拒绝错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13655850/
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-26 19:23:26  来源:igfitidea点击:

Access denied error in IE when submitting form through javascript

javascriptjqueryasp.net-mvcfile-upload

提问by Krish

@using (Html.BeginForm("Upload", "MyProfile", FormMethod.Post, new
{
    @encType = "multipart/form-data",
    id = "ImgForm",
    name = "ImgForm",
    target = "UploadTarget"
}))
{   
    <input type="file" name="FileUpload" class="filestyle-notext fileupload">                
}

<iframe id="UploadTarget" name="UploadTarget" style="position: absolute; left: -999em; top: -999em;"></iframe>

And through javascript/jquery, I am doing form submit on change of file input.

并通过 javascript/jquery,我正在提交更改文件输入的表单。

$('.myprofile .fileupload').change(function () {       
    $('#ImgForm').submit();
});

It throws an error: Access is denied and It happens only in IE (i am using ie8) and works fine in firefox, chrome.

它抛出一个错误:访问被拒绝,它仅在 IE 中发生(我使用的是 ie8)并且在 Firefox、chrome 中工作正常。

After reading in forums I see there is an issue with form submit through javasript in IE due to security reasons but is there any workaround ? And I don't understand why the hell only IE does that when all browsers are supporting it. Is IE more secure than all browsers ? ;) Pool in your suggestions please.

在论坛中阅读后,我发现由于安全原因,通过 IE 中的 javasript 提交表单存在问题,但有什么解决方法吗?而且我不明白为什么当所有浏览器都支持它时只有 IE 会这样做。IE 比所有浏览器都安全吗?;) 请在您的建议池。

回答by Darin Dimitrov

IE8 doesn't support invoking the .submit()event of a form containing file inputs from within the .change()event of this file input. This is for security reasons. You are attempting to submit a file to the server without the user explicitly allowing this. One possible workaround is to call the .submit()on the form from within a .click() event of some button you have placed:

IE8 不支持.submit().change()该文件输入的事件中调用包含文件输入的表单的事件。这是出于安全原因。您试图在没有用户明确允许的情况下向服务器提交文件。一种可能的解决方法是.submit()从您放置的某个按钮的 .click() 事件中调用表单:

$('.uploadButton').click(function () {       
    $('#ImgForm').submit();
});

Now when the user clicks on some upload button the form will submit.

现在,当用户单击某个上传按钮时,表单将提交。

It's worth mentioning that this is only problematic with IE8. Higher versions and other browsers do not have this limitation.

值得一提的是,这仅在 IE8 上有问题。更高版本和其他浏览器没有这个限制。

Also you may consider using a Flash based upload control such as Uploadifyor BlueImp File uploadto upload files to the server in a cross browser way.

您也可以考虑使用基于 Flash 的上传控件,例如UploadifyBlueImp File upload,以跨浏览器的方式将文件上传到服务器。