繁体   English   中英

CSV文件下载后,使用jQuery / Javascript打开“另存为”对话框

[英]Open Save As Dialog box using jQuery/Javascript after csv file download

我尝试在用户单击按钮后打开“另存为”对话框,但是需要将文件保存到下载文件夹。 我想提示用户将文件保存在哪里。

这是我到目前为止的Javascript函数:

function exportOBCSerialsToCSV(e) {
    var dataSource = $("#vehicleGrid").data("kendoGrid").dataSource;

    var filteredDataSource = new kendo.data.DataSource({
        data: dataSource.data(),
        filter: dataSource.filter()
    });

    filteredDataSource.read();

    var data = filteredDataSource.view();
    var result = '';

    for (var dataRow = 0; dataRow < data.length; dataRow++) {
        result += data[dataRow].OBCSerial + ',';
        if (dataRow == data.length - 1) {
            result += data[dataRow].OBCSerial;
        }
    }
    if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(new Blob([result]), 'OBC Serials.csv');
    }
    else if (window.URL != null) {
        var a = document.createElement('a');
        result = encodeURIComponent(result);
        a.href = 'data:application/csv;charset=UTF-8,' + result;
        a.download = 'OBC Serials.csv';
        a.click();
    }
    else {
        window.open(result);
    }
    e.preventDefault();
}

以下可能性:

  1. 设置服务器上文件的标头,如下所示:

 Below possibilities : 1. Set the header of the file on the server, like so: <FilesMatch "\\.(?i:pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> The download attribute does not allow you to change the filename or filetype any more as it is an obvious security risk. What you are trying to do it replicate the right-click - save-as dialogue but I'm afraid that is not possible at this time. 2. When user's browser is set to automatically download all files in default location which is why not only this file but all other files from user's browser were downloaded directly without the save prompt dialogue. Changing the settings in browser to 'always ask the download location' can work. 

download属性不允许您再更改文件名或文件类型,因为这显然存在安全风险。 您正在尝试执行的操作会复制右键单击-另存为对话框,但目前恐怕无法实现。

  1. 当用户的浏览器设置为自动在默认位置下载所有文件时,这就是为什么不仅下载此文件,而且直接从用户浏览器下载所有其他文件而没有保存提示对话框的原因。 将浏览器中的设置更改为“总是询问下载位置”即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM