JavaScript - 上传和保存文件 [英] JavaScript - Upload and save file

查看:69
本文介绍了JavaScript - 上传和保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过浏览器上传文件,然后将其保存到本地或网络文件夹中。我从 http:// muaz-khan获得了代码.blogspot.com / 2012/10 / save-files-on-disk-using-javascript-or.html [ ^ ],但保存的文件是快捷类型。关键功能代码如下。在此代码中,fileURL定义文件的来源,fileName是目标。

我希望有人可以为此问题提供良好的来源。提前致谢。

I tried to upload a file through a browser and then save it into a local or network folder. I got the code from http://muaz-khan.blogspot.com/2012/10/save-files-on-disk-using-javascript-or.html[^], but the saved file is a shortcut type. The key function code is below. In this code, the fileURL defines the source of the file, and the fileName is the destination.
I wish someone could provide a good source for this issue. Thanks in advance.

// http://muaz-khan.blogspot.com/2012/10/save-files-on-disk-using-javascript-or.html
function SaveToDisk(fileURL, fileName) {
    if (!window.ActiveXObject) {                // for non-IE
        var save = document.createElement('a');
        save.href = fileURL;
        save.target = '_blank';
        save.download = fileName || fileURL;
        var evt = document.createEvent('MouseEvents');
        evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
        save.dispatchEvent(evt);
        (window.URL || window.webkitURL).revokeObjectURL(save.href);
    }
    else if (!!window.ActiveXObject && document.execCommand) {  // for IE
        var _window = window.open(fileURL, "_blank");
        _window.document.close();
        _window.document.execCommand('SaveAs', true, fileName || fileURL)
        _window.close();
    }
}

推荐答案

检查:

< a href =https://github.com/eligrey/FileSaver.js> https://github.com/eligrey/FileSaver.js [ ^ ]



摘录自github:

FileSaver.js在本机不支持它的浏览器中实现HTML5 W3C saveAs()FileSaver接口。有一个FileSaver.js演示,演示了如何保存各种媒体类型。



FileSaver.js是在客户端保存文件的解决方案,非常适合webapps需要生成文件,或保存不应发送到外部服务器的敏感信息。



寻找canvas.toBlob()来保存画布?查看canvas-toBlob.js以获得跨浏览器实现。
Check this:
https://github.com/eligrey/FileSaver.js[^]

Excerpt from github:
FileSaver.js implements the HTML5 W3C saveAs() FileSaver interface in browsers that do not natively support it. There is a FileSaver.js demo that demonstrates saving various media types.

FileSaver.js is the solution to saving files on the client-side, and is perfect for webapps that need to generate files, or for saving sensitive information that shouldn't be sent to an external server.

Looking for canvas.toBlob() for saving canvases? Check out canvas-toBlob.js for a cross-browser implementation.


这篇关于JavaScript - 上传和保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆