C# 如何为下载的文件指定唯一名称
我有一个C#应用程序,它将一个完整的PDF文件保存在我网站的文件夹中。在操作过程中,我将两个会话变量保存到服务器中的文件名和文件路径:C# 如何为下载的文件指定唯一名称,c#,asp.net,C#,Asp.net,我有一个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()); /
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
链接按钮的功能是:
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;
}
}
}
使用制度;
使用System.Web;
公共类下载文件PDF:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
System.Web.HttpRequest request=System.Web.HttpContext.Current.request;
字符串stressvar=request.QueryString[“路径名”];
System.Web.HttpRequest request2=System.Web.HttpContext.Current.Request;
字符串stressvar2=request.QueryString[“fileName”];
System.Web.HttpResponse response=System.Web.HttpContext.Current.response;
response.ClearContent();
response.Clear();
response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=“+stressvar+”;”);
response.End();
}
公共布尔可重用{
得到{
返回false;
}
}
}
当我在服务器上运行我的网站时,它会要求我下载ASHX文件,但如果我从与服务器位于同一网络上的本地PC上运行我的网站,它会提示我下载PDF文件。到目前为止,一切都很好,但我遇到了两个问题:
它在我的电脑中下载的文件名是DownloadFilePDF
,它是HttpHandler文件名
该文件为0字节,当我打开该文件时,它不是正确的文件类型
我怎样才能使
文件名是我发送到HttpHandler
文件的filename
查询字符串
我可以下载驻留在服务器本身中的文件,因此它不是0字节
如何为下载的文件指定唯一名称
您可以使用两个选项,如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();
为了解决这个问题,jst将您的文件内容流到响应中,请看:
response.BinaryWrite(GetFileContentsFromSomewhere());
这里有点不对劲。我没有看到任何内容流式传输到客户端。您需要在响应中提供内容,如下所示:
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();
response.WriteFile(localPathOfFile);
或
你的例子就是我现在看到的。。。我希望下载的文件与服务器上的文件名保持一致(如果可能)。@SearchForkKnowledge您只是不从服务器发送任何文件!看看我的更新。实际上,这些文件保存在服务器的C:\驱动器上,我与所有人共享了该驱动器。因此,在服务器中,文件位于C:\PDFFolder\{filename}.pdf,从服务器外部,我将以\\myserver\PDFFolder\{filename}.pdf访问它。我如何修改我的代码来使用它?婴儿体内应该有什么/你的档案来自哪里?
response.WriteFile(localPathOfFile);
response.WriteFile(Server.MapPath(urlOfFile));