2016-10-03 60 views
0

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

这里是我的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(); 
} 
+0

您需要将一个Input Type =文件放入您的DOM中。 – Keith

+0

请你能提供更多关于如何完成的细节。点击下载按钮后,我想要提供另存为对话框。 – Majase

回答

0

以下可能性:

  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.

下载属性不允许您更改文件名或文件类型,因为这是一个明显的安全风险。 你正在尝试做什么它复制右键单击 - 另存为对话框,但恐怕这是不可能在这个时候。

  • 当用户的浏览器被设置为自动下载这就是为什么不仅这个文件,但是从用户的浏览器的所有其他文件直接下载无需保存提示对话框的默认位置的所有文件。将浏览器中的设置更改为“始终询问下载位置”即可使用。
  • +0

    我试过,但没有运气。我想单击下载按钮,并用另存为对话框提示。 – Majase

    相关问题