Javascript ActiveXObject 未定义且找不到变量:ActiveXObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11101641/
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
ActiveXObject is not defined and can't find variable: ActiveXObject
提问by Ramesh Lamani
i want to create text file in local, when i browse in Google chrome click of the button it is showing error like ActiveXObject is not definedand when i browse in safari click of the button it is showing error like can't find variable: ActiveXObject. any one can help me.how can i achieve and create file .Thanq
我想在本地创建文本文件,当我在谷歌浏览器中点击按钮时,它显示错误,如 ActiveXObject 未定义,当我在 safari 中浏览时,单击按钮显示错误,如 找不到变量:ActiveXObject. 任何人都可以帮助我。我如何实现和创建文件 .Thanq
<script>
function createFile() {
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.CreateTextFile("C:\Hello.txt", true);
file.WriteLine('Hello World');
alert('Filecreated');
file.WriteLine('Hope is a thing with feathers, that perches on the soul.');
file.Close();
}
</script>
<input type="Button" value="Create File" onClick='createFile()'>
回答by Fabrizio Calderan
ActiveXObject
is available only on IE browser. So every other useragent will throw an error
ActiveXObject
仅在 IE 浏览器上可用。所以每个其他用户代理都会抛出错误
On modern browser you could use instead File APIor File writer API(currently implemented only on Chrome)
在现代浏览器上,您可以使用File API或File writer API(目前仅在 Chrome 上实现)
回答by Quentin
ActiveXObject
is non-standard and only supported by Internet Explorer on Windows.
ActiveXObject
是非标准的,仅受 Windows 上的 Internet Explorer 支持。
There is no native cross browser way to write to the file system without using plugins, even the draft File APIgives read only access.
没有不使用插件的本地跨浏览器写入文件系统的方式,即使草案文件 API提供只读访问权限。
If you want to work cross platform, then you need to look at such things as signed Java applets (keeping in mind that that will only work on platforms for which the Java runtime is available).
如果您想跨平台工作,那么您需要查看诸如已签名的 Java 小程序之类的东西(请记住,这仅适用于 Java 运行时可用的平台)。
回答by jasssonpet
A web app can request access to a sandboxed file system by calling window.requestFileSystem()
. Works in Chrome.
Web 应用程序可以通过调用 来请求访问沙盒文件系统window.requestFileSystem()
。在 Chrome 中工作。
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var fs = null;
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function (filesystem) {
fs = filesystem;
}, errorHandler);
fs.root.getFile('Hello.txt', {
create: true
}, null, errorHandler);
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
More info here.
更多信息在这里。