我有一个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文件。到目前为止一切都很好,但是,我遇到了两个问题:
DownloadFilePDF
,即HttpHandler文件名。我该怎样修复......
fileName
文件的HttpHandler
QueryString。答案 0 :(得分:2)
如何为下载的文件提供唯一名称
您可以使用几个选项,例如Guid
类,DateTime.Now
方法等,以便为下载的文件提供唯一标识符,例如,使用Guid.NewGuid
:
response.AddHeader("Content-Disposition", "attachment; filename=" + string.format(strSessVar+{0}, Gui.NewGuid()) + ";");
<强>更新强>
通过使用以下代码,除了发送空文件外,您什么都不做:
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();
为了解决这个问题,请将您的文件内容传输到响应中,看看:
response.BinaryWrite(GetFileContentsFromSomewhere());
答案 1 :(得分:1)
这里有点不对劲。我没有看到任何内容流式传输到客户端。您需要在响应中提供内容,如下所示:
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.BinaryWrite(GetFileContentsFromSomewhere()); //<--- this baby does all the magic
response.End();
GetFileContentsFromSomewhere()实现取决于您希望文件来自何处。如果它只是您的Web服务器上的静态文件,您可以使用以下内容:
response.WriteFile(localPathOfFile);
或
response.WriteFile(Server.MapPath(urlOfFile));