我有一个ASP.net网站,它位于我的一个服务器中,我从同一网络上的本地PC访问该网站。应用程序运行时,它会将PDF文件保存在C:\PDFFolder
文件夹中。我想显示一个链接,以便用户可以从服务器下载本地PC中的文件。
我正在使用文件名的会话变量:
string strFileName = "completed_pdf_" + k + ".pdf"; //k is a variable within the function
Session["fileName"] = strFileName;
在服务器中,存储文件的文件夹是:C:\PDFFolder\{filename}.pdf
从本地PC访问该文件是:\\myserver\PDFFolder\{filename}.pdf
DOWNLOAD按钮的代码是:
protected void DownloadFile(object sender, EventArgs e)
{
//MessageBox.Show(Session["pathName"].ToString());
//MessageBox.Show(Session["fileName"].ToString());
MessageBox.Show(Path.Combine(@"C:\PDFGenerate", Session["fileName"].ToString()).ToString());
Response.Redirect("DownloadFilePDF.ashx?myvar=" + Session["fileName"].ToString());
}
在下载文件的按钮上,我点击HttpHandler
功能点击:
<%@ WebHandler Language="C#" Class="DownloadFilePDF" %>
using System;
using System.Web;
using System.IO;
public class DownloadFilePDF : IHttpHandler {
public void ProcessRequest (HttpContext context) {
System.Web.HttpRequest request2 = System.Web.HttpContext.Current.Request;
string strSessVar2 = request2.QueryString["fileName"];
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
byte[] fileByteArray = File.ReadAllBytes(Path.Combine(@"C:\PDFGenerate", strSessVar2));
response.AddHeader("Content-disposition", String.Format("attachment; filename={0}.pdf", strSessVar2));
response.BinaryWrite(fileByteArray);
response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
我一直在这一行收到错误:byte[] fileByteArray = File.ReadAllBytes(Path.Combine(@"C:\PDFGenerate", strSessVar2));
错误是:Value cannot be null
如何解决问题?
答案 0 :(得分:2)
我相信你的问题就在这一行:
string strSessVar2 = request2.QueryString["fileName"];
您希望获得myvar
的价值:
string strSessVar2 = request2.QueryString["myvar"];