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

Download file from server folder to client pc

本文关键字:客户端 pc 下载 文件夹 文件 服务器      更新时间:2023-09-26

如何将文件(.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的文件,而不是的全部内容

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

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