【问题标题】:.msg file gives download error.msg 文件给出下载错误
【发布时间】:2012-03-31 20:37:03
【问题描述】:

在我的应用程序中,我将一些文件保存在服务器上,并在某些业务逻辑上使它们可供下载。

正在下载所有其他文件类型,但 .msg(Outlook message) 文件未下载并给出错误:

404 - File or directory not found. The resource you are looking for might
have been removed, had its name changed, or is temporarily unavailable.

图像、.docx、.txt 所有其他文件都运行良好。

该页面是在 ASP.NET 中设计的,并且在客户端站点上进行了标记。

【问题讨论】:

标签: c# asp.net outlook http-status-code-404


【解决方案1】:

在 ASP.NET 上找到 forum

创建一个处理程序,将其下载为文件:

Response.ContentType = "application/vnd.ms-outlook";
Response.AppendHeader("Content-Disposition","attachment; filename=Message.msg");
Response.TransmitFile(Server.MapPath(YourPathToMsgFile));
Response.End();

或更改 IIS 6.0 中的设置:

选择 HTTP Header -> 点击 MIME types -> 点击 New 并添加“.msg”作为扩展名和“application/vnd.ms-outlook”作为 MIME 类型。

【讨论】:

  • 在 IIS 7.5 中,单击服务器,然后在 IIS 部分中选择“Mime Types”,然后单击右侧操作栏下的“添加...”(或在列表中单击鼠标右键) mime 类型)。
【解决方案2】:

使用下面这个标签,我们可以直接在标签中提到文件名。

   <a href="Your File_Location">Download Link</a>

无需在控制器中指定代码。

只需将下面的标签添加到 web.config 里面

  <staticContent>
    <mimeMap fileExtension=".msg" mimeType="application/octet-stream" />
</staticContent>

【讨论】:

  • 请编辑您的答案,并解释为什么这样可以解决问题。
【解决方案3】:
<system.webServer>   
    <staticContent>
      <mimeMap fileExtension=".msg" mimeType="application/octet-stream" />
    </staticContent>
</system.webServer>

【讨论】:

  • 能否请您为您的答案添加更多上下文。仅代码的答案很难理解。如果您可以在帖子中添加更多信息,这将对提问者和未来的读者都有帮助。
【解决方案4】:

这是我在 ASP.NET 论坛上找到的另一个回复。包括在此处以节省时间。

如果 ASP.NET Core 自己处理静态内容并在边缘运行,或者如果您需要 ASP.NET Core 了解 mime 类型,则需要配置 ASP.NET Core 的处理程序以了解它使用 FileExtensionContentTypeProvider 如下:

public void Configure(IApplicationBuilder app)
{
    // Set up custom content types - associating file extension to MIME type
    var provider = new FileExtensionContentTypeProvider();
    // Replace an existing mapping
    provider.Mappings[".msg"] = "application/vnd.ms-outlook";

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
        RequestPath = "/StaticContentDir",
        ContentTypeProvider = provider
    });

感谢陈雪莉

ASPNET Forum LINK

【讨论】:

  • 我最亲爱的朋友 Avi,你的回答完全正确,但有一个问题。在生产中(至少 IIS 10),函数 Directory.GetCurrentDirectory() 返回“c:\windows\system32\inetsrv”导致错误。即使文档引用了这个确切的代码。我已将此行更改为“Path.Combine(env.WebRootPath,“images”)”,然后在生产中一切正常。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多