javascript 除 IE 之外的其他浏览器的 ActiveX 对象的替代方案?

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

Alternative for the ActiveX object for the other browsers excluding IE?

javascriptjquerycsv

提问by pavan

Can anyone help me as i am stuck in creating a file(temporary) with extension(say .csv) in the local disk, i tried using ActiveX object but it works fine only in internet explorer,got a error when i tried with other browsers. By googling i got to know ActiveX works only in IE so it produces error when its tried in different browser.

任何人都可以帮助我,因为我被困在本地磁盘中创建带有扩展名(例如 .csv)的文件(临时),我尝试使用 ActiveX 对象,但它仅在 Internet Explorer 中工作正常,当我尝试使用其他浏览器时出错. 通过谷歌搜索,我了解到 ActiveX 仅在 IE 中有效,因此在不同浏览器中尝试时会产生错误。

    var fso, f1;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f1 = fso.GetFile("c:\test.txt");
    alert(f1);

Can anyone suggest me with the alternative which can be very useful to.

任何人都可以向我建议非常有用的替代方案。

采纳答案by webnoob

The other posts cover all the information about storing to the local machine using Html 5 so I won't go into any more detail about that but this is only going to work on Html 5 compatible browsers (although most browsers are Html 5 compatible now, I don't know your project requirements).

其他帖子涵盖了有关使用 Html 5 存储到本地计算机的所有信息,因此我不会详细介绍这些信息,但这仅适用于兼容 Html 5 的浏览器(尽管现在大多数浏览器都兼容 Html 5,我不知道你的项目要求)。

Another alternative would be to create / save the file using server side code (ASP.NET, PHP etc) and then provide a link to the user so they can download the file. You can still save this as a .csv file so that the user can open it in Excel after they have downloaded it.

另一种选择是使用服务器端代码(ASP.NET、PHP 等)创建/保存文件,然后向用户提供链接,以便他们可以下载文件。您仍然可以将其保存为 .csv 文件,以便用户在下载后可以在 Excel 中打开它。

Edit:

编辑:

What I would do is change your web service so that it saves the file to disc (more info: here). Then instead of returning the file from the web service, return the Url to download it instead. You can then redirect the user to that link using JS.

我要做的是更改您的网络服务,以便将文件保存到光盘(更多信息:此处)。然后,不是从 Web 服务返回文件,而是返回 Url 来下载它。然后,您可以使用 JS 将用户重定向到该链接。

回答by Elias Van Ootegem

The problem you have is that browser implementations of ECMAScript (JavaScript) don't allow you to write to the clients' disk. Microsoft reverse-engineered JavaScript, and dubbed it JScript. Though the base is the same, MS added those ActiveX objects, and a compiler (JScript can be compiled, yes).

您遇到的问题是 ECMAScript (JavaScript) 的浏览器实现不允许您写入客户端磁盘。Microsoft 对 JavaScript 进行了逆向工程,并将其称为JScript。虽然基础是一样的,但MS添加了那些ActiveX对象和一个编译器(JScript可以编译,是的)。

In short, JScript can be used for more than just browser tasks, so MS provided ActiveX objects to enable some form of I/O. The only thing you can do on browsers otherthan IE, is using HTML5's new DOM Storage objects. Teemu provided a link to the documentation for that

简而言之,JScript 不仅仅可以用于浏览器任务,因此 MS 提供了 ActiveX 对象来启用某种形式的 I/O。你可以在浏览器中做的唯一的事情其他比IE,使用HTML5的新的DOM存储对象。Teemu 为此提供了指向文档的链接