如何设置blob数据的下载文件扩展名
How to set the download file extension for blob data
在我的站点视频中使用了blob数据,当下载视频时,保存的文件名是Chrome浏览器中扩展名为.txt
(4671addc-3ce0-4eb6-b414-ddf3406b1fe5.txt
)的blob名称,而在 Firefox 中带有 .mp4
(4671addc-3ce0-4eb6-b414-ddf3406b1fe5.mp4
) 扩展名。
如何指定下载文件的扩展名和文件名。
我尝试使用以下代码进行设置,但 none 有效。
type="video/mp4"
filename="111.mp4"
download="111.mp4"
这是我现在使用的示例代码。
<video
width="300px"
id="video"
type="video/mp4"
filename="111.mp4"
download="111.mp4"
controls=""
src="blob:http://localhost/4671addc-3ce0-4eb6-b414-ddf3406b1fe5">
</video>
使用 HTML5 download
属性下载 Blob
URL 文件
Notice
download
属性仅适用于 HTML5 a
或 area
标签 ✅
download
HTML5 video
标签上不存在属性 ❌
下载Blob
URL图片
<section>
<img id="img" />
<a id="img-link" download>...loading</a>
</section>
// ES5
function generatorBlobURL(url, type, dom, link) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(res) {
var blob = new Blob(
[xhr.response],
{'type' : type},
);
var urlBlob = URL.createObjectURL(blob);
// render `blob` url ✅
dom.src = urlBlob;
// using `a` tag download ✅
link.href = urlBlob;
link.innerText = urlBlob;
link.download = filename;
};
xhr.send();
}
(function() {
var type = 'image/png';
var url = 'https://cdn.xgqfrms.xyz/logo/icon.png';
var dom = document.querySelector('#img');
var link = document.querySelector('#img-link');
var arr = url.split('/');
var filename = arr[arr.length - 1] || 'default-filename';
generatorBlobURL(url, type, dom, link, filename);
})();
下载Blob
URL视频
<section>
<video id="video" controls width="400" height="300">
loading...
</video>
<br>
<a id="video-link" download>...loading</a>
</section>
// ES5
function generatorBlobURL(url, type, dom, link) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(res) {
var blob = new Blob(
[xhr.response],
{'type' : type},
);
var urlBlob = URL.createObjectURL(blob);
// render `blob` url ✅
dom.src = urlBlob;
// using `a` tag download ✅
link.href = urlBlob;
link.innerText = urlBlob;
link.download = filename;
};
xhr.send();
}
(function() {
var type = 'video/mp4';
var url = 'https://cdn.xgqfrms.xyz/HTML5/Blob/2022-04-07-sh.mp4';
var dom = document.querySelector('#video');
var link = document.querySelector('#video-link');
var arr = url.split('/');
// arr.at(-1);
var filename = arr[arr.length - 1] || 'default-filename';
setTimeout(() => {
generatorBlobURL(url, type, dom, link, filename);
}, 0);
})();
现场演示
https://codepen.io/xgqfrms/full/YzYRLwg
screenshots
参考资料
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes
在我的站点视频中使用了blob数据,当下载视频时,保存的文件名是Chrome浏览器中扩展名为.txt
(4671addc-3ce0-4eb6-b414-ddf3406b1fe5.txt
)的blob名称,而在 Firefox 中带有 .mp4
(4671addc-3ce0-4eb6-b414-ddf3406b1fe5.mp4
) 扩展名。
如何指定下载文件的扩展名和文件名。
我尝试使用以下代码进行设置,但 none 有效。
type="video/mp4"
filename="111.mp4"
download="111.mp4"
这是我现在使用的示例代码。
<video
width="300px"
id="video"
type="video/mp4"
filename="111.mp4"
download="111.mp4"
controls=""
src="blob:http://localhost/4671addc-3ce0-4eb6-b414-ddf3406b1fe5">
</video>
使用 HTML5 download
属性下载 Blob
URL 文件
Notice
download
属性仅适用于 HTML5 a
或 area
标签 ✅
download
HTML5 video
标签上不存在属性 ❌
下载Blob
URL图片
<section>
<img id="img" />
<a id="img-link" download>...loading</a>
</section>
// ES5
function generatorBlobURL(url, type, dom, link) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(res) {
var blob = new Blob(
[xhr.response],
{'type' : type},
);
var urlBlob = URL.createObjectURL(blob);
// render `blob` url ✅
dom.src = urlBlob;
// using `a` tag download ✅
link.href = urlBlob;
link.innerText = urlBlob;
link.download = filename;
};
xhr.send();
}
(function() {
var type = 'image/png';
var url = 'https://cdn.xgqfrms.xyz/logo/icon.png';
var dom = document.querySelector('#img');
var link = document.querySelector('#img-link');
var arr = url.split('/');
var filename = arr[arr.length - 1] || 'default-filename';
generatorBlobURL(url, type, dom, link, filename);
})();
下载Blob
URL视频
<section>
<video id="video" controls width="400" height="300">
loading...
</video>
<br>
<a id="video-link" download>...loading</a>
</section>
// ES5
function generatorBlobURL(url, type, dom, link) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(res) {
var blob = new Blob(
[xhr.response],
{'type' : type},
);
var urlBlob = URL.createObjectURL(blob);
// render `blob` url ✅
dom.src = urlBlob;
// using `a` tag download ✅
link.href = urlBlob;
link.innerText = urlBlob;
link.download = filename;
};
xhr.send();
}
(function() {
var type = 'video/mp4';
var url = 'https://cdn.xgqfrms.xyz/HTML5/Blob/2022-04-07-sh.mp4';
var dom = document.querySelector('#video');
var link = document.querySelector('#video-link');
var arr = url.split('/');
// arr.at(-1);
var filename = arr[arr.length - 1] || 'default-filename';
setTimeout(() => {
generatorBlobURL(url, type, dom, link, filename);
}, 0);
})();
现场演示
https://codepen.io/xgqfrms/full/YzYRLwg
screenshots
参考资料
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes