使用webservice和jquery ajax在客户端下载excel文件 [英] Download excel file on client side using webservice and jquery ajax
问题描述
我想在服务器端的excel中导出数据,并在点击按钮时将该excel文件下载到客户端。我创建了一个Web服务方法,并从jQuery进行了ajax调用。在Web服务中,我能够创建excel并将其存储到服务器端模块,但我无法在客户端下载该文件。我不知道怎么做?谁能帮我?我附上了我已经完成的代码。
我尝试过:
网络服务代码
[WebMethod]
[ScriptMethod(UseHttpGet) = false ,ResponseFormat = ResponseFormat.Json)]
public string ExportReport( int SelectedValue, string KeyValue, string DdlCltVal, string DdlLocVal, string DdlstfVal, int BtnID,DateTime StrDate,DateTime EndDate, int currentPage)
{
try
{
CommonFunction obj = new CommonFunction();
DataTable dt = new DataTable();
string rhead = ;
if (SelectedValue!= 0 && KeyValue == 0&& DdlCltVal == 0&& DdlLocVal == 0&& DdlstfVal == 0)
{
CourierReportController objCtr = new CourierReportController();
dt = ListToDataTable.ToDataTable(objCtr.GetDailyReport( 0 , 10 ,SelectedValue));
rhead = 每日;
}
StringBuilder sb = new StringBuilder();
foreach (DataColumn column in dt.Columns)
{
sb.Append(column.ColumnName + \t);
}
sb.Append( \ n);
foreach (DataRow row in dt.Rows)
{
for ( int i = 0 ; i < dt.Columns.Count; i ++)
{
sb.Append(row [i] .ToString()+ \t);
}
sb.Append( \ n);
}
string strFilename = CourierReport _ + rhead + 。xls;
string strUploadPath = Server.MapPath( userexports跨度>)的ToString();
File.WriteAllText(strUploadPath + \\ + strFilename,sb.ToString ());
return strFilename;
}
catch (例外情况)
{
throw ex;
}
}
客户端代码:
$(' #btnExport')。click( function (e){
var data = JSON2.stringify( {
SelectedValue:selectedValue,
KeyValue:KeyValue,
DdlCltVal:ddlCltVal,
DdlLocVal:ddlCltVal,
DdlstfVal:ddlstfVal,
BtnID:btnid,
StrDate:strDate,
EndDate:endDate,
currentPage:currentPage
});
$ .ajax({
contentType: application / json; charset = utf-8,
类型:' POST',url:' http:// localhost:8043 / Modules / CourierReport / services / CourierReport.asmx / ExportReport',
dataType:' json',
data:data,
success: function (结果){
alert(result.d);
// 我应该在这做什么吗?
},
错误: function (错误){
alert( 错误跨度>);
}
});
return false ;
});
(' #btnExport')。click( function (e){
var data = JSON2.stringify({
SelectedValue:selectedValue,
KeyValue:KeyValue,
DdlCltVal:ddlCltVal,
DdlLocVal:ddlCltVal,
DdlstfVal :ddlstfVal,
BtnID:btnid,
StrDate:strDate,
EndDate:endDate,
currentPage:currentPage
});
I want to export data in excel in server side and download that excel file to client side on button click . I created a web service method and made an ajax call from jQuery. In the web service, I was able to create excel and store it to the server side module but I am unable to download the file on client side. I've no idea how to do it? Can any one help me? I've attached code what I've done. 这篇关于使用webservice和jquery ajax在客户端下载excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< blockquote> .ajax({
contentType: application / json; charset = utf-8,
类型:' POST',url:' http:// localhost:8043 / Modules / CourierReport / services / CourierReport.asmx / ExportReport',
dataType:' json' ,
数据:数据,
成功:功能(结果){
alert(result.d);
// 我应该在这做什么吗?
},
错误: function (错误){
alert( 错误跨度>);
}
});
return false ;
});
你将无法使用这样的代码,javascript无法将文件保存到本地驱动器,而你只是返回服务器上存在的文件名。谷歌如何下载文件到客户端,你会发现例子
在ASP.NET中使用另存为对话框下载文件 - Rick Strahl的Web日志 [ ^ ]
根据建议,您需要将客户端指向包含下载代码的链接,例如
< a href =MyDownloadPage.aspxtarget =_ blank>下载< / a>
,MyDownloadPage根据链接中的代码发送文件。
What I have tried:
Web service code
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string ExportReport(int SelectedValue, string KeyValue, string DdlCltVal, string DdlLocVal, string DdlstfVal, int BtnID, DateTime StrDate, DateTime EndDate, int currentPage)
{
try
{
CommonFunction obj = new CommonFunction();
DataTable dt = new DataTable();
string rhead = "";
if (SelectedValue != 0 && KeyValue == "0" && DdlCltVal == "0" && DdlLocVal == "0" && DdlstfVal == "0")
{
CourierReportController objCtr = new CourierReportController();
dt = ListToDataTable.ToDataTable(objCtr.GetDailyReport(0, 10, SelectedValue));
rhead = "Daily";
}
StringBuilder sb = new StringBuilder();
foreach (DataColumn column in dt.Columns)
{
sb.Append(column.ColumnName + "\t");
}
sb.Append("\n");
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sb.Append(row[i].ToString() + "\t");
}
sb.Append("\n");
}
string strFilename = "CourierReport_" + rhead + ".xls";
string strUploadPath = Server.MapPath("userexports").ToString();
File.WriteAllText(strUploadPath + "\\" + strFilename, sb.ToString());
return strFilename;
}
catch (Exception ex)
{
throw ex;
}
}
Client side code:
$('#btnExport').click(function (e) {
var data= JSON2.stringify({
SelectedValue: selectedValue,
KeyValue: KeyValue,
DdlCltVal: ddlCltVal,
DdlLocVal: ddlCltVal,
DdlstfVal: ddlstfVal,
BtnID:btnid,
StrDate: strDate,
EndDate: endDate,
currentPage: currentPage
});
$.ajax({
contentType: "application/json; charset=utf-8",
type: 'POST', url:'http://localhost:8043/Modules/CourierReport/services/CourierReport.asmx/ExportReport',
dataType: 'json',
data:data,
success: function(result){
alert (result.d);
//should i do any thing here?
},
error: function(error){
alert("error");
}
});
return false;
});
('#btnExport').click(function (e) {
var data= JSON2.stringify({
SelectedValue: selectedValue,
KeyValue: KeyValue,
DdlCltVal: ddlCltVal,
DdlLocVal: ddlCltVal,
DdlstfVal: ddlstfVal,
BtnID:btnid,
StrDate: strDate,
EndDate: endDate,
currentPage: currentPage
});
.ajax({
contentType: "application/json; charset=utf-8",
type: 'POST', url:'http://localhost:8043/Modules/CourierReport/services/CourierReport.asmx/ExportReport',
dataType: 'json',
data:data,
success: function(result){
alert (result.d);
//should i do any thing here?
},
error: function(error){
alert("error");
}
});
return false;
});
You won't be able to use code like that, javascript can't save files to the local drive, and you're just returning a filename that exists on the server. Google for how to download a file to the client and you'll find examples
Downloading a File with a Save As Dialog in ASP.NET - Rick Strahl's Web Log[^]
As suggested already, you need to direct the client to a link that has your download code, like
<a href="MyDownloadPage.aspx" target="_blank">Download</a>
with the MyDownloadPage sending the file as per the code in the link.