[JS] jQuery代码自动下载文件

原理:使用代码创建form,并添加属性,自动提交–>实现下载文件

function download(filepath) {
    var url = "http://localhost:8080/DonwloadFileServlet/DownloadFileServlet";

    var form = $("<form></form>").attr("action", url).attr("method", "post");
    form.append($("<input></input>").attr("type", "hidden").attr("name", "filepath").attr("value", filepath));
    form.appendTo('body').submit().remove();
}

猜你喜欢

转载自blog.csdn.net/summer_dew/article/details/80715768