问题描述
>新建File()构造函数在chromium / chrome 38+中可用。请参阅, Well, the files attribute of an input[type=file] is read-only. Therefore I can not write my blob data into this input element. But if I create a new input file element using Javscript, then possible to insert blob data on creation? I am only interested in solutions working in chrome (extension) - other browsers do not matter. new File() constructor is available at chromium / chrome 38+. See File Constructor Sample , File API. 这篇关于Chrome:用Javascript创建blob文件输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
var date = new Date(),
filename = "file-" + date.getTime() + ".html";
var generatedFile = new File(
["<!DOCTYPE html><html><body>" + filename + "</body></html>"]
, filename
, {
type: "text/html",
lastModified: date
}
);
var objUrl = URL.createObjectURL(generatedFile);
console.log(generatedFile, objUrl);
var reader = new FileReader();
reader.addEventListener("load", function(event) {
console.log(event.target.result)
});
reader.readAsText(generatedFile);