C# 如何通过jQueryAjax和C下载文件#
我想用jqueryajaxweb方法下载一个文件,但它不起作用 以下是我对web方法的jQuery ajax调用:C# 如何通过jQueryAjax和C下载文件#,c#,jquery,webmethod,C#,Jquery,Webmethod,我想用jqueryajaxweb方法下载一个文件,但它不起作用 以下是我对web方法的jQuery ajax调用: function GenerateExcel() { var ResultTable = jQuery('<div/>').append(jQuery('<table/>').append($('.hDivBox').find('thead').clone()).append($('.bDiv').find('tbody').clone()));
function GenerateExcel() {
var ResultTable = jQuery('<div/>').append(jQuery('<table/>').append($('.hDivBox').find('thead').clone()).append($('.bDiv').find('tbody').clone()));
var list = [$(ResultTable).html()];
var jsonText = JSON.stringify({ list: list });
$.ajax({
type: "POST",
url: "GenerateMatrix.aspx/GenerateExcel",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response.d);
}
});
}
函数GenerateExcel(){
var ResultTable=jQuery(“”).append(jQuery(“”).append($('.hDivBox').find('thead').clone()).append($('.bDiv').find('tbody').clone());
var list=[$(可结果).html();
var jsonText=JSON.stringify({list:list});
$.ajax({
类型:“POST”,
url:“GenerateMatrix.aspx/GenerateExcel”,
数据:jsonText,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(响应){
},
故障:功能(响应){
警报(response.d);
}
});
}
这是web方法定义:
[System.Web.Services.WebMethod()]
public static string GenerateExcel(List<string> list)
{
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=FileEName.xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Write(list[0]);
HttpContext.Current.Response.End();
return "";
}
[System.Web.Services.WebMethod()]
公共静态字符串GenerateExcel(列表)
{
HttpContext.Current.Response.AppendHeader(“内容处置”、“附件;文件名=filename.xls”);
HttpContext.Current.Response.Charset=“”;
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType=“应用程序/vnd.ms excel”;
HttpContext.Current.Response.Write(列表[0]);
HttpContext.Current.Response.End();
返回“”;
}
怎么做
还有一件事:我想在客户端PC上下载它,而不是保存在服务器上。假设C#代码使用正确的Excel标题进行响应,您可以简单地重定向到链接,而不是使用ajax:
var list = [$(ResultTable).html()];
var url = "GenerateMatrix.aspx/GenerateExcel";
var data = {list: list};
url += '?' + decodeURIComponent($.param(data));
// if url is an excel file, the browser will handle it (should show a download dialog)
window.location = url;
我已经用iframe完成了 这是修改后的ajax函数调用
function GenerateExcel() {
var ResultTable = jQuery('<div/>').append(jQuery('<table/>').append($('.hDivBox').find('thead').clone()).append($('.bDiv').find('tbody').clone()));
var list = [$(ResultTable).html()];
var jsonText = JSON.stringify({ list: list });
$.ajax({
type: "POST",
url: "GenerateMatrix.aspx/GenerateExcel",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (isNaN(response.d) == false) {
$('#iframe').attr('src', 'GenerateMatrix.aspx?ExcelReportId=' + response.d);
$('#iframe').load();
}
else {
alert(response.d);
}
},
failure: function (response) {
alert(response.d);
}
});
}
<iframe id="iframe" style="display:none;"></iframe>
<button id="download_file">Download</button>
<script>
$('#download_file').click(function(){
var path = 'e-payment_format.pdf';//name of the file
$("#iframe").attr("src", "/FileCabinet/Download?file=" + path);
});
</script>
$(“#下载_文件”)。单击(函数(){
var path='e-payment_format.pdf';//文件名
$(“#iframe”).attr(“src”,“/FileCabinet/Download?file=“+path”);
});
那应该行 如果你直接去链接会发生什么(没有ajax)?看下面的帖子:@Lian我不能直接去链接,因为我必须从客户端发送html。你可以看看这个问题,这可能会有帮助。web方法会保持不变吗?我不知道你的输出是什么。检查这个:我用的是同样的东西,但是你的答案对meHi@rahul不起作用,你的方法就是我要找的,但是我对.hDivbox、b.Div和表结构的时候感到很困惑?在开瓶器窗口吗?我将Iframe设置为display:block,但没有执行任何操作。谢谢
<iframe id="iframe" style="display:none;"></iframe>
<button id="download_file">Download</button>
public string Download(string file)
{
string filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["FileManagementPath"]);
string actualFilePath = System.IO.Path.Combine(filePath, file);
HttpContext.Response.ContentType = "APPLICATION/OCTET-STREAM";
string filename = Path.GetFileName(actualFilePath);
String Header = "Attachment; Filename=" + filename;
HttpContext.Response.AppendHeader("Content-Disposition", Header);
HttpContext.Response.WriteFile(actualFilePath);
HttpContext.Response.End();
return "";
}
<script>
$('#download_file').click(function(){
var path = 'e-payment_format.pdf';//name of the file
$("#iframe").attr("src", "/FileCabinet/Download?file=" + path);
});
</script>