我的WPF应用程序从Microsoft.Win32.OpenFileDialog()获取用户的文件......
Private Sub ButtonUpload_Click(...)
Dim FileOpenStream As Stream = Nothing
Dim FileBox As New Microsoft.Win32.OpenFileDialog()
FileBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
FileBox.Filter = "Pictures (*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png|" & _
"Documents (*.pdf;*.doc;*.docx;)|*.pdf;*.doc;*.docx;|" & _
"All Files (*.*)|*.*"
FileBox.FilterIndex = 1
FileBox.Multiselect = False
Dim FileSelected As Nullable(Of Boolean) = FileBox.ShowDialog(Me)
If FileSelected IsNot Nothing AndAlso FileSelected.Value = True Then
Try
FileOpenStream = FileBox.OpenFile()
If (FileOpenStream IsNot Nothing) Then
Dim ByteArray As Byte()
Using br As New BinaryReader(FileOpenStream)
ByteArray = br.ReadBytes(FileOpenStream.Length)
End Using
Dim z As New ZackFile
z.Id = Guid.NewGuid
z.FileData = ByteArray
z.FileSize = CInt(ByteArray.Length)
z.FileName = FileBox.FileName.Split("\").Last
z.DateAdded = Now
db.AddToZackFile(z)
db.SaveChanges()
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. " & Ex.Message, "Fail", MessageBoxButton.OK, MessageBoxImage.Error)
Finally
If (FileOpenStream IsNot Nothing) Then
FileOpenStream.Close()
End If
End Try
End If
End Sub
我的ASP.NET MVC应用程序使用FileStreamResult()在网站上下载...
Public Class ZackFileController
Inherits System.Web.Mvc.Controller
Function Display(ByVal id As Guid) As FileStreamResult
Dim db As New EfrDotOrgEntities
Dim Model As ZackFile = (From z As ZackFile In db.ZackFile _
Where z.Id = id _
Select z).First
Dim ByteArray As Byte() = Model.ImageData
Dim FileStream As System.IO.MemoryStream = New System.IO.MemoryStream(ByteArray)
Dim ContentType As String = ?????
Dim f As New FileStreamResult(FileStream, ContentType)
f.FileDownloadName = Model.FileName
Return f
End Function
End Class
但是FileStreamResult()需要一个内容类型字符串。我如何知道我的文件的正确内容类型?
答案 0 :(得分:20)
.NET 4.5中有一个MimeMapping类
http://msdn.microsoft.com/en-us/library/system.web.mimemapping.aspx
答案 1 :(得分:15)
我已使用ContentType列替换了数据库表中的FileExtension列。
我上传文件时填充它。
Private Sub ButtonUpload_Click(...)
...
Dim FileExtension As String = "." + FileBox.FileName.Split(".").Last.ToLower
z.ContentType = ContentType(FileExtension)
...
End Sub
我使用此功能确定内容类型:
Function ContentType(ByVal FileExtension As String) As String
Dim d As New Dictionary(Of String, String)
'Images'
d.Add(".bmp", "image/bmp")
d.Add(".gif", "image/gif")
d.Add(".jpeg", "image/jpeg")
d.Add(".jpg", "image/jpeg")
d.Add(".png", "image/png")
d.Add(".tif", "image/tiff")
d.Add(".tiff", "image/tiff")
'Documents'
d.Add(".doc", "application/msword")
d.Add(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
d.Add(".pdf", "application/pdf")
'Slideshows'
d.Add(".ppt", "application/vnd.ms-powerpoint")
d.Add(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation")
'Data'
d.Add(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
d.Add(".xls", "application/vnd.ms-excel")
d.Add(".csv", "text/csv")
d.Add(".xml", "text/xml")
d.Add(".txt", "text/plain")
'Compressed Folders'
d.Add(".zip", "application/zip")
'Audio'
d.Add(".ogg", "application/ogg")
d.Add(".mp3", "audio/mpeg")
d.Add(".wma", "audio/x-ms-wma")
d.Add(".wav", "audio/x-wav")
'Video'
d.Add(".wmv", "audio/x-ms-wmv")
d.Add(".swf", "application/x-shockwave-flash")
d.Add(".avi", "video/avi")
d.Add(".mp4", "video/mp4")
d.Add(".mpeg", "video/mpeg")
d.Add(".mpg", "video/mpeg")
d.Add(".qt", "video/quicktime")
Return d(FileExtension)
End Function
这很有效,但似乎不够优雅。
答案 2 :(得分:5)
我在web.config中使用自定义配置部分(我将完成自定义配置部分的任务留给您):
<configSections>
<section name="mimeTypeMappings" type="MySoftwareLol.MimeTypeMappingSection, MySoftwareLol"/>
</configSections>
<mimeTypeMappings>
<MimeTypes>
<MimeType Type="application/mac-binhex40" Extensions=".hqx"/>
<MimeType Type="application/msword" Extensions=".doc"/>
<MimeType Type="application/pdf" Extensions=".pdf"/>
<MimeType Type="application/postscript" Extensions=".ai;.eps;.ps"/>
<MimeType Type="application/rtf" Extensions=".rtf"/>
<MimeType Type="application/vnd.ms-excel" Extensions=".xla;.xlc;.xlm;.xls;.xlt;.xlw"/>
<MimeType Type="application/vnd.ms-outlook" Extensions=".msg"/>
<MimeType Type="application/vnd.ms-powerpoint" Extensions=".pot;.pps;.ppt"/>
<MimeType Type="application/vnd.ms-works" Extensions=".wcm;.wdb;.wks;.wps"/>
<MimeType Type="application/x-compress" Extensions=".z"/>
<MimeType Type="application/x-compressed" Extensions=".tgz"/>
<MimeType Type="application/x-gzip" Extensions=".gz"/>
<MimeType Type="application/x-msaccess" Extensions=".mdb"/>
<MimeType Type="application/x-msmetafile" Extensions=".wmf"/>
<MimeType Type="application/x-mspublisher" Extensions=".pub"/>
<MimeType Type="application/x-msschedule" Extensions=".scd"/>
<MimeType Type="application/x-msterminal" Extensions=".trm"/>
<MimeType Type="application/x-mswrite" Extensions=".wri"/>
<MimeType Type="application/x-tar" Extensions=".tar"/>
<MimeType Type="application/zip" Extensions=".zip"/>
<MimeType Type="audio/basic" Extensions=".au;.snd"/>
<MimeType Type="audio/mid" Extensions=".mid;.rmi"/>
<MimeType Type="audio/mpeg" Extensions=".mp3"/>
<MimeType Type="audio/x-aiff" Extensions=".aif;.aifc;.aiff"/>
<MimeType Type="audio/x-pn-realaudio" Extensions=".ra;.ram"/>
<MimeType Type="audio/x-wav" Extensions=".wav"/>
<MimeType Type="image/bmp" Extensions=".bmp"/>
<MimeType Type="image/gif" Extensions=".gif"/>
<MimeType Type="image/jpeg" Extensions=".jpe;.jpeg;.jpg"/>
<MimeType Type="image/pipeg" Extensions=".jfif"/>
<MimeType Type="image/tiff" Extensions=".tif;.tiff"/>
<!--By default, sends HTML documents as plain text; sending it as text/html may pose security risks-->
<!--If you wish to accept the risks, uncomment the following two lines and comment out the third.-->
<!--<MimeType Type="text/html" Extensions=".mht;.html;.htm"/>-->
<!--<MimeType Type="text/plain" Extensions=".txt"/>-->
<MimeType Type="text/plain" Extensions=".txt;.html;.htm"/>
<MimeType Type="text/richtext" Extensions=".rtx"/>
<MimeType Type="text/tab-separated-values" Extensions=".tsv"/>
<MimeType Type="video/mpeg" Extensions=".mp2;.mpa;.mpe;.mpeg;.mpg;.mpv2"/>
<MimeType Type="video/quicktime" Extensions=".mov;.qt"/>
<MimeType Type="video/x-la-asf" Extensions=".lsf;.lsx;.asf;.asr;.asx;"/>
<MimeType Type="video/x-msvideo" Extensions=".avi"/>
</MimeTypes>
</mimeTypeMappings>
我使用Path.GetExtension()并将其映射到Extensions中列出的其中一种类型。
答案 3 :(得分:4)
我根据Zacks响应创建了一个C#helper类。
/// <summary>
/// Helps with Mime Types
/// </summary>
public static class MimeTypesHelper
{
/// <summary>
/// Returns the content type based on the given file extension
/// </summary>
public static string GetContentType(string fileExtension)
{
var mimeTypes = new Dictionary<String, String>
{
{".bmp", "image/bmp"},
{".gif", "image/gif"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".png", "image/png"},
{".tif", "image/tiff"},
{".tiff", "image/tiff"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".pdf", "application/pdf"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".xls", "application/vnd.ms-excel"},
{".csv", "text/csv"},
{".xml", "text/xml"},
{".txt", "text/plain"},
{".zip", "application/zip"},
{".ogg", "application/ogg"},
{".mp3", "audio/mpeg"},
{".wma", "audio/x-ms-wma"},
{".wav", "audio/x-wav"},
{".wmv", "audio/x-ms-wmv"},
{".swf", "application/x-shockwave-flash"},
{".avi", "video/avi"},
{".mp4", "video/mp4"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".qt", "video/quicktime"}
};
// if the file type is not recognized, return "application/octet-stream" so the browser will simply download it
return mimeTypes.ContainsKey(fileExtension) ? mimeTypes[fileExtension] : "application/octet-stream";
}
}
当然,您可能希望将mimeTypes
保留为将来的查询。这只是一个起点。
答案 4 :(得分:3)
可以使用注册表项轻松完成。
Dim regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("extension")
If Not regKey Is Nothing Then
Dim ct As Object = regKey.GetValue("Content Type")
If Not ct Is Nothing Then
Return ct.ToString()
End If
End If
这将产生与上面提到的大清单相同的结果!!!!
答案 5 :(得分:2)
当您设置内容类型时,您仍然拥有文件名。您可以为文件扩展名选择正确的mime类型,或默认为“application / base64”。
假设下载它的人将使用网络浏览器,请尝试坚持使用常见的浏览器:MIME Types Known By IE
答案 6 :(得分:0)
您可以使用This列表,其中包含684个文件扩展名。
在此列表中,每个对用“〜”分隔,每对都有扩展名,内容类型用“#”分隔。
我不知道你使用的语言是什么,但这是在vb.net
如何使用它:
将此类添加到项目中(将字符串解析为Dictionary)
Public Class MIMETypeList
Public data As Dictionary(Of String, String)
Public Sub New()
data = New Dictionary(Of String, String)()
Dim Temp() As String
For Each TypeConvertion As String In My.Resources.MimeType.Split("~")
Temp = TypeConvertion.Split("#")
If Not data.ContainsKey(Temp(0)) Then
data.Add(Temp(0), Temp(1))
End If
Next
End Sub
End Class
现在您可以像这样使用它:
Dim FileExtension1 As String = ".mp4"
Dim FileExtension2 As String = ".html"
Dim FileExtension3 As String = ".Random"
Dim Converter As New MIMETypeList
If Converter.data.ContainsKey(FileExtension1) Then
Debug.WriteLine(FileExtension1 & " -> " & Converter.data.Item(FileExtension1))
Else
Debug.WriteLine(FileExtension1 & " -> File Content Not Found")
End If
If Converter.data.ContainsKey(FileExtension2) Then
Debug.WriteLine(FileExtension2 & " -> " & Converter.data.Item(FileExtension2))
Else
Debug.WriteLine(FileExtension2 & " -> File Content Not Found")
End If
If Converter.data.ContainsKey(FileExtension3) Then
Debug.WriteLine(FileExtension3 & " -> " & Converter.data.Item(FileExtension3))
Else
Debug.WriteLine(FileExtension3 & " -> File Content Not Found")
End If
输出:
.mp4 - &gt;视频/ MP4
.html - &gt; text / html的
.Random - &gt;找不到文件内容