将文件从服务器文件夹下载到客户端PC

时间:2016-07-30 23:00:44

标签: javascript

如何从服务器上的可公开访问的文件夹下载文件(.mp3)到客户端PC?

我试过了:

let url = "\\public\\test.mp3";
let xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
    var a = document.createElement('a');
    // xhr.response is a blob
    a.href = window.URL.createObjectURL(xhr.response); 
    a.download = 'test.mp3';
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
};

xhr.open('GET', url);
xhr.send();

但是这只是用15KB而不是整个文件下载文件

1 个答案:

答案 0 :(得分:0)

只需将用户导航到mp3文件:

window.location.href = '\\public\\test.mp3';