8

我目前正在使用 HttpResponse 从我的服务器下载文件。我已经有几个函数用于下载 Excel/Word 文件,但我无法下载我的简单文本文件 (.txt)。

对于文本文件,我基本上是将 TextBox 的内容转储到文件中,尝试使用 HttpResponse 下载文件,然后删除临时文本文件。

这是适用于 Excel/Word 文档的代码示例:

protected void linkInstructions_Click(object sender, EventArgs e)
{
    String FileName = "BulkAdd_Instructions.doc";
    String FilePath = Server.MapPath("~/TempFiles/BulkAdd_Instructions.doc");
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "application/x-unknown";
    response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
    response.TransmitFile(FilePath);
    response.Flush();
    response.End();  
}

这是不起作用的代码块。
请注意,代码运行时不会引发任何错误。文件已创建并删除,但从未转储给用户。

protected void saveLog(object sender, EventArgs e)
{ 
    string date = DateTime.Now.ToString("MM_dd_yyyy_hhmm");     //  Get Date/Time
    string fileName = "BulkLog_"+ date + ".txt";                //  Stitch File Name + Date/Time
    string logText = errorLog.Text;                             //  Get Text from TextBox
    string halfPath = "~/TempFiles/" + fileName;                //  Add File Name to Path
    string mappedPath = Server.MapPath(halfPath);               //  Create Full Path

    File.WriteAllText(mappedPath, logText);                     //  Write All Text to File

    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "text/plain";
    response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    response.TransmitFile(mappedPath);                //  Transmit File
    response.Flush();

    System.IO.File.Delete(mappedPath);                //  Delete Temporary Log
    response.End();
}
4

6 回答 6

11

这是因为您在发送之前删除了文件。

来自 MSDN - HttpResponse.End 方法

将所有当前缓冲的输出发送到客户端,停止页面的执行,并引发 EndRequest 事件。

试着把你的 System.IO.File.Delete(mappedPath); 在 response.End(); 之后的行 在我的测试中,它似乎正在工作。

此外,最好先检查文件是否存在,看不到任何文件。那里存在,不想要任何空引用异常,并设置 Content-Length。

编辑:这是我不久前在工作项目中使用的代码,可能会对您有所帮助。

// Get the physical Path of the file
string filepath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + folder + filename;

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(filepath);

// Checking if file exists
if (file.Exists)
{                            
    // Clear the content of the response
    Response.ClearContent();

    // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", file.Name));

    // Add the file size into the response header
    Response.AddHeader("Content-Length", file.Length.ToString());

    // Set the ContentType
    Response.ContentType = ReturnFiletype(file.Extension.ToLower());

    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
    Response.TransmitFile(file.FullName);

    // End the response
    Response.End();

    //send statistics to the class
}

这是我使用的 Filetype 方法

//return the filetype to tell the browser. 
//defaults to "application/octet-stream" if it cant find a match, as this works for all file types.
public static string ReturnFiletype(string fileExtension)
{
    switch (fileExtension)
    {
        case ".htm":
        case ".html":
        case ".log":
            return "text/HTML";
        case ".txt":
            return "text/plain";
        case ".doc":
            return "application/ms-word";
        case ".tiff":
        case ".tif":
            return "image/tiff";
        case ".asf":
            return "video/x-ms-asf";
        case ".avi":
            return "video/avi";
        case ".zip":
            return "application/zip";
        case ".xls":
        case ".csv":
            return "application/vnd.ms-excel";
        case ".gif":
            return "image/gif";
        case ".jpg":
        case "jpeg":
            return "image/jpeg";
        case ".bmp":
            return "image/bmp";
        case ".wav":
            return "audio/wav";
        case ".mp3":
            return "audio/mpeg3";
        case ".mpg":
        case "mpeg":
            return "video/mpeg";
        case ".rtf":
            return "application/rtf";
        case ".asp":
            return "text/asp";
        case ".pdf":
            return "application/pdf";
        case ".fdf":
            return "application/vnd.fdf";
        case ".ppt":
            return "application/mspowerpoint";
        case ".dwg":
            return "image/vnd.dwg";
        case ".msg":
            return "application/msoutlook";
        case ".xml":
        case ".sdxl":
            return "application/xml";
        case ".xdp":
            return "application/vnd.adobe.xdp+xml";
        default:
            return "application/octet-stream";
    }
}
于 2011-03-30T01:02:04.583 回答
5

我在搜索中偶然发现了这篇文章,并注意到它在告诉我们为什么 UpdatePanel 首先导致问题时没有用。

UpdatePanel 是异步回发,而 Response.TransmitFile 需要完整回发才能正常工作。

触发异步回发的控件需要在UpdatePanel中做一个触发器:

<Triggers>        
<asp:PostBackTrigger ControlID="ID_of_your_control_that_causes_postback" />
</Triggers>
于 2016-09-14T14:05:17.973 回答
2

感谢您跟进您的问题。我花了好几个小时试图弄清楚为什么没有任何错误代码被抛出,尽管没有发生任何事情。原来是我的 AJAX UpdatePanel 神秘而隐蔽地妨碍了我。

于 2011-04-07T19:28:05.837 回答
1

我解决了这个问题。Response对象需要完整的发才能下载服务器上生成的 Excel 文件。但我的网络表单上的UpdatePanel阻止了完整的回发,其中包含我的导出按钮。所以.. 在UpdatePanel标签内,我改变了这个......

<asp:AsyncPostBackTrigger ControlID="btnExport" EventName="Click" />

...为此解决问题:

<asp:PostBackTrigger ControlID="btnExport"/>
于 2021-02-17T16:22:35.740 回答
0

也可以尝试客户端(现在仅限 Chrome)上保存文本,而无需往返服务器。

是另一个闪光基地...

于 2014-02-05T12:22:19.297 回答
-6

我最终自己解决了这个问题。事实证明这是一个 Ajax 问题,不允许我的 Button 正确回发。这阻止了 TransmitFile 被触发。

谢谢您的帮助!

于 2011-03-30T21:45:15.980 回答