【发布时间】:2014-08-17 18:47:43
【问题描述】:
我有一个 C# 应用程序,它将完成的 PDF 文件保存在我网站内的文件夹中。在操作过程中,我将两个会话变量保存到服务器中的文件名和文件路径:
string strFileName = "completed_pdf_" + k + ".pdf"; //k is a variable in a function for the name
Session["fileName"] = strFileName;
MessageBox.Show(Session["fileName"].toString()); //displays: completed_pdf_{name}.pdf
newFileServer = System.Environment.MachineName + @"/PDFGenerate/completed_pdf_" + k + ".pdf";
strFullPath = Path.GetFullPath("//" + newFileServer);
List<System.Web.UI.WebControls.ListItem> files = new List<System.Web.UI.WebControls.ListItem>();
files.Add(new System.Web.UI.WebControls.ListItem(strFullPath, strFullPath));
strN = files[0].ToString();
Session["pathName"] = strN;
MessageBox.Show(Session["pathName"].toString()); //displays: \\myserver\pdfgen\completed_pdf_{name}.pdf
我有一个GridView,它显示一个LinkButton:
<asp:LinkButton ID="lnkDownload" Text = "Download" runat="server" OnClick = "DownloadFile" />
LinkButton 的函数是:
protected void DownloadFile(object sender, EventArgs e)
{
//MessageBox.Show(Session["pathName"].ToString()); //displays correctly
//MessageBox.Show(Session["fileName"].ToString()); //displays correctly
Response.Redirect("DownloadFilePDF.ashx?myvar=" + Session["pathName"].ToString() + "&myvar2=" + Session["fileName"].ToString());
}
我的HTTPHandler 代码是这样的:
<%@ WebHandler Language="C#" Class="DownloadFilePDF" %>
using System;
using System.Web;
public class DownloadFilePDF : IHttpHandler {
public void ProcessRequest (HttpContext context) {
System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
string strSessVar = request.QueryString["pathName"];
System.Web.HttpRequest request2 = System.Web.HttpContext.Current.Request;
string strSessVar2 = request.QueryString["fileName"];
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + strSessVar + ";");
response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
当我在服务器本身中运行我的网站时,它会要求我下载 ASHX 文件,但如果我从与服务器位于同一网络的本地 PC 上运行我的网站,它会提示我下载 PDF 文件。到目前为止一切都很好,但是,我遇到了两个问题:
- 它在我的 PC 中下载的文件名是
DownloadFilePDF,它是 HttpHandler 文件名。 - 文件为 0 字节,当我打开文件时,文件类型不正确。
我该如何解决这个问题..
- 文件名是
fileNameQueryString 我发送到HttpHandler文件。 - 我可以下载驻留在服务器本身的文件,所以它不是 0 字节。
【问题讨论】: