Skip to main content
 首页 » 编程设计

c#之为什么下载后文件类型变了

2024年02月20日13langtianya

我在 Downloads.aspx 页面的 Page_Load 处理程序中设置了 Response.ContentType = "application/octet-stream";。当我尝试下载 .gif 文件时,它工作正常。但对于 .jpg 文件,对话框显示文件类型为“application/octet-stream”,下载的文件只是一个不是预期的 .jpg 的文件。

请您参考如下方法:

尝试为每种文件类型设置明确的内容类型。

switch (fileExtension) 
{ 
    case "gif": Response.ContentType = "image/gif"; break;  
    case "jpeg": Response.ContentType = "image/jpg"; break;  
    case "jpg": Response.ContentType = "image/jpg"; break;  
    case "png": Response.ContentType = "image/png"; break;  
 
    default: Response.ContentType = "application/octet-stream"; break;  
} 

请参阅以下链接以获取 mime 类型引用:

http://www.freeformatter.com/mime-types-list.html 
https://github.com/cymen/ApacheMimeTypesToDotNet/blob/master/ApacheMimeTypes.cs