我想將一些數據寫入csv文件,用戶應該可以通過瀏覽器下載。使用struts 2作爲csv下載
@Actions({ @Action(value = "/downloadReport", results = { @Result(name = "success", type = "stream", params = {
"contentType", "${type}", "inputName", "stream", "bufferSize","2048", "contentDisposition",
"attachment;filename=\"${filename}\"" }) }) })
@SkipValidation
public String downloadReport() throws BaseAppException {
try {
filename =AdSkippingConstants.REPORT_FILE_NAME;
type = AdSkippingConstants.CSV_FILE_TYPE;
File file = new File(filename);
FileUtils.write(file, "Helloo World");
stream = new FileInputStream(file);
}
catch (IOException e) {
logger.error("Error occured while exporting error data", e);
}
return SUCCESS;
}
在JSP中我使用Ajax調用
function exportAsCSV(){
$.ajax({
url: 'downloadReport.action?',
type: "POST",
success: function() {
//To Un Block the Change Password page if the page reloaded
//$('div.ui-dialog').unblock();
// $('#change_password_details_body').html(data);
},
error: function(){
//To Un Block the Change Password page if the page reloaded with error.
//$('div.ui-dialog').unblock();
}
});
}
我能得到響應,甚至fileDownload =真正的響應來,但還是下載到CSV選項未在任何瀏覽器中打開和也請讓我知道如何通過html數據寫入csv的行動。
在JSP中我使用這個代碼做Ajax調用
var gridModel = "gridModel";
var sortname = "1";
var sortorder = "asc";
var caption = '<s:text name="grid.label.heading" />';
var url = "getGridSearchResults.action";
init.push(function() {
loadTable("gridtable", url, gridModel, columnDefs, columns, sortname,
sortorder, caption);
});
<table cellpadding="0" cellspacing="0" border="0" class="table table- striped table-bordered" id="gridtable"></table>
因此,與Ajax調用我加載的數據表。
但是它是Ajax,沒有對話框會彈出標準的Ajax調用。 –
那麼我該如何在jsp中調用動作並獲取下載作爲csv工作 –
@PracheerPancholi您是否在所有瀏覽器或只有IE瀏覽器中都有此問題? –