admin管理员组文章数量:1421572
function download(){
var fileName = 'testDemo';
var type = 'xyz';
var data = 'Hello world';
var file = new Blob([data], {
type: type
});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, fileName);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = fileName + '.' +type;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
}
<button onclick='download()'>Download</button>
function download(){
var fileName = 'testDemo';
var type = 'xyz';
var data = 'Hello world';
var file = new Blob([data], {
type: type
});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, fileName);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = fileName + '.' +type;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
}
<button onclick='download()'>Download</button>
Here I am setting type and extension as .xyz
but still it shows plain text document (text/plain)
in property and can't read the file type also which shows a empty string. How can I create a custom extension and type?
- Damn, i wish someone had answered this one - i'll try get back here if I find a solution to my problem. I was looking for a way to make a custom format to store the smallest picture ever, like this: delimiter, x1y1x2y2x3y3x4y4 delimiter x1xy2 etc, such that the delimiters denote swapping to the next color and the palette is stored seperately – Aj Otto Commented Sep 4, 2018 at 23:11
1 Answer
Reset to default 3You are passing an extension instead of useful mime type at the time of instantiating blob object.
var fileName = 'testDemo';
var type = 'application/pdf';
var data = 'Hello world';
var file = new Blob([data], {
type: type
});
Refer other useful mime types here : mime types
本文标签: javascriptCreate custom file type and extension using Blob()Stack Overflow
版权声明:本文标题:javascript - Create custom file type and extension using Blob() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745345655a2654464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论