【问题标题】:How to give a downloaded file unique name如何为下载的文件提供唯一名称
【发布时间】: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 文件。到目前为止一切都很好,但是,我遇到了两个问题:

  1. 它在我的 PC 中下载的文件名是 DownloadFilePDF,它是 HttpHandler 文件名。
  2. 文件为 0 字节,当我打开文件时,文件类型不正确。

我该如何解决这个问题..

  1. 文件名是fileName QueryString 我发送到HttpHandler 文件。
  2. 我可以下载驻留在服务器本身的文件,所以它不是 0 字节。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如何为下载的文件指定唯一名称

    您可以使用几个选项,如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());
    

    【讨论】:

    • 你的例子就是我现在所拥有的......我希望下载的文件保持与服务器上相同的文件名(如果可能的话)。
    • @SearchForKnowledge 您根本不从您的服务器发送任何文件!看看我的更新。
    • 实际上文件保存在我与大家共享的服务器的 C:\ 驱动器上。所以在服务器中,该文件位于 C:\PDFFolder\{filename}.pdf 并且从服务器外部我会以 \\myserver\PDFFolder\{filename}.pdf 的形式访问它。如何修复我的代码以使用它?
    【解决方案2】:

    这里有些不对劲。我没有看到任何内容正在流式传输到客户端。您需要在响应中提供内容,如下所示:

    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() 实现取决于您希望文件来自何处。如果它只是你的网络服务器上的一个静态文件,你可以使用这样的东西:

    response.WriteFile(localPathOfFile);
    

    response.WriteFile(Server.MapPath(urlOfFile));
    

    【讨论】:

    • 宝宝里面应该有什么? ://
    • 您的文件来自哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多