继承人的情景:
是否可以从javascript(从外国网页执行)保存数据而无需从服务器下载文件?
任何帮助将非常感激!
当我想在不使用服务器的情况下启动下载时,我遇到了这种情况.我写了这个jQuery插件,它在Blob中包含了textarea/div的内容,然后启动了Blob的下载.允许您指定文件名和类型..
jQuery.fn.downld = function (ops) {
this.each(function () {
var _ops = ops || {},
file_name = _ops.name || "downld_file",
file_type = _ops.type || "txt",
file_content = $(this).val() || $(this).html();
var _file = new Blob([file_content],{type:'application/octet-stream'});
window.URL = window.URL || window.webkitURL;
var a = document.createElement('a');
a.href = window.URL.createObjectURL(_file);
a.download = file_name+"."+file_type;
document.body.appendChild(a);
a.click(); $('a').last().remove();
});
}
Run Code Online (Sandbox Code Playgroud)
默认使用:$("#element").downld();
选项:$("#element").downld({name:"some_file_name",type:"html"});
Codepen示例http://codepen.io/anon/pen/cAqzE
| 归档时间: |
|
| 查看次数: |
7040 次 |
| 最近记录: |