【问题标题】:Open Save As Dialog box using jQuery/Javascript after csv file download下载 csv 文件后使用 jQuery/Javascript 打开另存为对话框
【发布时间】:2026-01-24 07:40:01
【问题描述】:

我试图在用户单击按钮后打开另存为对话框,但它需要文件到下载文件夹。我想提示用户保存文件的位置。

这是我目前拥有的 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();
}

【问题讨论】:

  • 您需要将 Input Type=file 放入您的 DOM。
  • 请您提供更多关于它是如何完成的细节。单击下载按钮后,我希望收到“另存为”对话框的提示。

标签: javascript jquery asp.net-mvc c#-4.0


【解决方案1】:

以下可能性:

  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.

下载属性不允许您再更改文件名或文件类型,因为这是明显的安全风险。 您正在尝试执行的操作复制了右键单击 - 另存为对话框,但恐怕目前不可能。

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

【讨论】:

  • 我试过了,但没有运气。我想点击下载按钮并提示另存为对话框。