【问题标题】:In ASP.NET is there a different way that a file must be transmitted in Firefox?在 ASP.NET 中是否有不同的方式必须在 Firefox 中传输文件?
【发布时间】:2010-09-17 10:12:49
【问题描述】:

我正在使用 ASP.NET 传输 .jar 文件。此代码在 IE 上完美运行。但是在 Firefox 上,文件下载已损坏。修复它的最佳方法是什么?下面是我正在使用的代码。

private void TransferFile()
{
    try
    {
        string filePath = Server.MapPath("SomeJarFIle.jar");

        FileInfo file = new FileInfo(filePath);

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

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

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

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

            // Write the file into the response
            //Response.TransmitFile(file.FullName);
            Response.WriteFile(file.FullName);

            // End the response
            Response.End();
        }
        else
        {
            this.Response.Write("Error in finding file.  Please try again.");
            this.Response.Flush();
        }
    }
    catch (Exception ex)
    {
        this.Response.Write(string.Format("Error: {0}", ex.Message));
    }
}



private string ReturnExtension(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";
        case ".jar":
            return "application/java-archive";
        default:
            return "application/octet-stream";
    }
} 

更新:

我添加了类型

case ".jar":
    return "application/java-archive";

这并没有解决问题。如果我把 .jar 文件压缩起来,它就可以正常传输了。

当我再次测试我的 localhost 时,我确实注意到文件下载没有问题。但是,当我将其推送到 Web 服务器时,就会出现问题。

【问题讨论】:

    标签: asp.net firefox file


    【解决方案1】:

    我在 ReturnExtension() 函数中没有看到“.jar”的情况(我认为将其命名为“ReturnMimetype”可能会更好)。这可能是问题所在,还是您只是忘记粘贴?

    .jar 的 mimetype 应该是 application/java-archive。详情在这里:http://en.wikipedia.org/wiki/Jar-file

    我认为这是问题所在。我记得当我传输一个 .docx 文件(它实际上是一个扩展名为 .jar 文件的不同扩展名的 zip 文件)时遇到了相同类型的问题。下载在 IE 中运行良好,但 Firefox 损坏了它。解决方案是发送正确的 mimetype。

    【讨论】:

      【解决方案2】:

      我只看到两件事,您似乎没有 .jar 文件扩展名的 mime 类型。

      两个我个人使用 writefile 而不是传输,但我不确定有什么区别。

      【讨论】:

      【解决方案3】:

      使用 Response.WriteFile 或 Response.BinaryWrite,TransmitFile 方法有一些已知的奇怪行为。

      【讨论】:

      • 是否有机会提供记录这些已知奇怪行为的链接?进行快速 Google 搜索并没有找到任何相关信息。
      • @ckittel - 不知道它是否仍然适用 - 有一段时间了。只记得在很多搜索的长尾中找到它,而我遇到的类似问题是通过放弃 TransmitFile 解决的。
      猜你喜欢
      • 1970-01-01
      • 2012-04-21
      • 2010-10-11
      • 2020-10-27
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多