我正在开发 ASP.NET Web 表单应用程序。我的要求是在用户单击给定链接时显示 excel 文件下载弹出框(此链接在弹出页面上,而不是在 aspx 页面上。)。我有一个带有链接的 aspx 页面。当用户点击时,它会调用 js 函数,通过我们调用 web 服务方法来生成弹出屏幕的 html。
代码:
function showListModelFromGenrator(divId) {
var lowner = $("#" + hdnLoggedInOwnerID)[0].value;
var sowner = $("#" + hdnSelectedOwnnerID)[0].value;
var commID = $("#" + hdnCommunityId)[0].value;
var controlid = '#' + divId;
$.ajax({
url: baseUrl + '/' + "WebServices/ExtraInfoWebService.asmx/GetProductActivityStatus",
data: { LoggedInOwnerId: lowner, SelectedOwnerId: sowner, CommunityId: commID },
success: function (response) {
$(controlid).dialog("destroy");
$(controlid).dialog({
autoOpen: false,
modal: true,
width: 560,
height: 370,
resizable: false
}).empty().append(response.text);
$(controlid).dialog('open');
var busyBox = new BusyBoxWrapper()
busyBox.HideBusyBoxAfter(5);
},
cache: false
});
}
网络方法:
[WebMethod(EnableSession = true)]
public string GetProductActivityStatus(int LoggedInOwnerId, int SelectedOwnerId, int CommunityId)
{
StringBuilder stringAuditStatus = new StringBuilder();
Audit objdata = new Audit();
try
{
DataTable dt = new DataTable();
int ownerID = LoggedInOwnerId;
if (SelectedOwnerId != 0)
ownerID = SelectedOwnerId;
dt = objdata.GetListmodeldata(ownerID, CommunityId);
stringAuditStatus.Append(@"<table><tr class=addressTableHeader><td>Code</td>" +
"<td>Description</td>" +
"<td>Status</td>" +
"<td>Date</td></tr>");
foreach (DataRow item in dt.Rows)
{
stringAuditStatus.Append(
"<tr><td>" + item["Code"] + "</td>" +
"<td>" + item["Description"] + "</td>" +
"<td>" + item["Status"] + "</td>" +
"<td>" + item["Date"] + "</td></tr>");
}
stringAuditStatus.Append("</table>");
stringAuditStatus.Append("<a id=lnkViewProductCodeStatus runat='Server' href='#' onclick='javascript:ExportExel();'>DownloadListModel</a>");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return stringAuditStatus.ToString();
}
当用户点击“lnkViewProductCodeStatus”(上面由网络方法创建)时。我们调用 JS 函数ExportExcel
,该函数调用处理程序方法来处理要下载的文件。
function ExportExel(){
var abc;
$.ajax({
type: "POST",
url: baseUrl + '/' + "WebServices/ExtraInfoWebService.asmx/Urlhttphandler",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//window.open(msg.d);
$.ajax({
type: "POST",
url: msg.d,
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//window.open(msg.d);
}
});
}
});
public void ProcessRequest(HttpContext context)
{
string FullFilePath = context.Server.MapPath("~/Certificates/" + "ExcelFile.xls");
System.IO.FileInfo file = new System.IO.FileInfo(FullFilePath);
if (file.Exists)
{
//For more MIME types list http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx
context.Response.ContentType = MIMETypeUtility.MIMETypeDescription(MIMEType.Excel);
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");
context.Response.AddHeader("Content-Length", file.Length.ToString());
context.Response.WriteFile(file.FullName);
context.Response.Flush();
context.Response.End();
}
}
当我调试应用程序调用正确到处理程序但文件下载弹出窗口没有出现时。我在页面上尝试的相同代码(不是在弹出窗口上)它工作正常。谁能指导我为什么这在我的情况下不起作用。
非常感谢,普拉尚特