我想从服务器下载文件,一旦用户完成下载操作,我想从服务器中删除它。因此,为此我使用以下Jquery插件:
fileDownload
我的想法是我将在其回调函数上删除文件。您可以在下面找到代码段:
$.fileDownload(url, {
contentType: "application/octet-stream",
contentDisposition: 'attachment; filename=' + response.fileName
})
.done(function () {
console.log('File download a success!');
})
.fail(function () {
console.log('File download failed!');
});
因此,当用户尝试下载文件时,主要发生以下错误:
资源解释为文档但使用MIME类型video / mp4传输。
所以,主要有两个问题:
或者,如果还有其他替代方法可以实现这一目标吗?感谢
答案 0 :(得分:1)
或者,如果还有其他替代方法可以实现这一目标吗?感谢
尝试将XMLHttpRequest
设置为responseType
"blob"
function refocus() {
document.body.removeChild(document.getElementById("download"));
console.log("File download a success!");
window.removeEventListener("focus", refocus)
}
var request = new XMLHttpRequest();
request.open("GET", "/path/to/mp4", true);
request.responseType = "blob";
request.onload = function (e) {
if (this.status === 200) {
// create `objectURL` of `this.response` : `.mp4` as `Blob`
var file = URL.createObjectURL(this.response);
var a = document.createElement("a");
a.href = file;
a.download = this.response.name || "file-" + new Date().getTime();
a.id = "download";
document.body.appendChild(a);
a.click();
// remove `a` following `Save As` dialog,
// `window` regains `focus`
window.addEventListener("focus", refocus, false);
} else {
console.log("File download failed!")
}
};
request.send();