Javascript 在 IE8 上访问被拒绝错误

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

getting access is denied error on IE8

javascriptfile-uploadinternet-explorer-8cross-domainaccess-denied

提问by deostroll

I have a HTML form (upload.htm) with a HTML file upload control inside of it.

我有一个 HTML 表单 ( upload.htm),其中包含一个 HTML 文件上传控件。

<form id="frmupload" name="upload" enctype="multipart/form-data" action="uploadhandler.ashx" method="post">
    <input id="uploader" name="uploadctrl" type="file"/>
</form>

There is also one JavaScript method in the above page which goes like:

上面页面中还有一个 JavaScript 方法,如下所示:

function performUpload(){
    document.getElementById('frmupload').submit();
}

I call this inside of a page (uploadpage.htm) from within an iframe:

我在一个页面(uploadpage.htm)中调用它iframe

<iframe id="docframe" src="upload.htm" style="display:none"></iframe>

I try to execute the statement shown below from the uploadpage.htmpage:

我尝试从uploadpage.htm页面执行下面显示的语句:

var i = document.getElementById('docframe');
i.contentWindow.performUpload();

I get an error saying Access is denied, and my debugger halts at the first JavaScript function I've shown. Both the files are in the same location in the web project. They have the same domain name too. Why do I get this error then?

我收到一条错误消息,说Access is denied,并且我的调试器在我展示的第一个 JavaScript 函数处停止。这两个文件位于 Web 项目中的同一位置。他们也有相同的域名。为什么我会收到这个错误?

Of course, earlier, I could post the page: when I did not set the nameattribute for the HTML upload control. But after I set the name attribute in HTML markup, I get this weird error. Why didn't I get this the first time?

当然,之前我可以发布页面:当我没有name为HTML上传控件设置属性时。但是在 HTML 标记中设置 name 属性后,我收到了这个奇怪的错误。为什么我第一次没有得到这个?

Had a look @ this post --> "Access is denied" when script tries to access iframe in IE8, but it didn't help.

看看@this post--> “访问被拒绝”当脚本尝试在 IE8 中访问 iframe 时,但它没有帮助。

回答by Dunc

IE doesn't allow manipulation of the type="file" input element from javascript due to security reasons. Setting the filename or invoking a click event to show the browser dialog will result in an "Access is denied" error on the form submit - Internet Explorer is clever about remembering what methods have been invoked.

由于安全原因,IE 不允许从 javascript 操作 type="file" 输入元素。设置文件名或调用单击事件以显示浏览器对话框将导致表单提交时出现“访问被拒绝”错误 - Internet Explorer 很聪明地记住调用了哪些方法。

Similar issue: http://www.webdeveloper.com/forum/showthread.php?t=181272

类似问题:http: //www.webdeveloper.com/forum/showthread.php?t=181272

回答by Rajat

Traditionally, JavaScript access to HTML input type="file"is severely limited for security concerns. AFAIK, You can't do the following with JS on a file uploader element:

传统上,input type="file"出于安全考虑,JavaScript 对 HTML 的访问受到严重限制。AFAIK,您不能在文件上传器元素上使用 JS 执行以下操作:

  1. You cannot read the "value" of the element as it holds the filename.
  2. You can't fire up the file selection menu via JS.
  3. You can't fire submit of the file uploader control via JS.
  1. 您无法读取元素的“值”,因为它包含文件名。
  2. 您无法通过 JS 启动文件选择菜单。
  3. 您不能通过 JS 触发文件上传器控件的提交。

All this is in place to prevent malicious attacks like stealing your files in background via JS. I haven't played with an input type="file"element in a while but my best guess is you will hit similar issues (if not all) in other browsers as well.

所有这些都是为了防止恶意攻击,例如通过 JS 在后台窃取您的文件。我有input type="file"一段时间没有玩过一个元素,但我最好的猜测是你也会在其他浏览器中遇到类似的问题(如果不是全部)。

Your best bet is a Flash based solution or maybe some new HTML5 control.

您最好的选择是基于 Flash 的解决方案或一些新的 HTML5 控件。

Also, here is an official reference on the subject matter:

此外,这里是有关该主题的官方参考:

http://msdn.microsoft.com/en-us/library/ms535263(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ms535263(v=vs.85).aspx

Check "Community Content" all the way on the bottom of the above page.

一直勾选上页底部的“社区内容”。

回答by Gaurav Saxena

In both the HTML files — the one which is in the frame and another which contains the frame — try adding document.domain='example.com'where 'example.com' is your domain name.

在这两个 HTML 文件中——一个在框架中,另一个包含框架——尝试添加document.domain='example.com'“example.com”是你的域名。