下载时提供文件的Mime类型. [英] Provide Mime type of a file while downloading.
本文介绍了下载时提供文件的Mime类型.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下载文件时提供哑剧类型的最佳方法是什么?我正在使用一个下载页面,该页面会将二进制内容和必要的标头写入响应流.
我尝试了以下方法.
1.使用注册表项
What is the best way to provide mime type while downloading a file? I am using a download page which will write binary content and necessary headers to the response stream.
I have tried the following approaches.
1. Using Registry Key
private string GetMimeType(FileInfo fileInfo)
{
string mimeType = "application/unknown";
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(fileInfo.Extension.ToLower());
if(regKey != null)
{
object contentType = regKey.GetValue("Content Type");
if(contentType != null)
mimeType = contentType.ToString();
}
return mimeType;
}
2.使用urlmon.dll
2. Using urlmon.dll
[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static System.UInt32 FindMimeFromData(
System.UInt32 pBC,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
System.UInt32 cbSize,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
System.UInt32 dwMimeFlags,
out System.UInt32 ppwzMimeOut,
System.UInt32 dwReserverd
);
这两种方法没有用,因为它无法识别生产服务器的许多扩展.您还有其他建议/想法吗?
These two methods were not useful as its not recognizing a lot of extensions from production server. Do you have any other suggestions/thoughts?
推荐答案
我只会在服务器上存储支持的mime类型以及文件扩展名.或只存储mime类型并将其添加到文件表中.
I would just store the mime types that I support, with a lookup of file extensions, on the server. Or just store the mime type and add it to the file table.
这篇关于下载时提供文件的Mime类型.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文