gpt4 book ai didi

.net - 如何在 .NET 中确定文件的内容类型?

转载 作者:行者123 更新时间:2023-12-04 01:17:48 26 4
gpt4 key购买 nike

我的 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() 需要一个内容类型字符串。我如何知道我的文件的正确内容类型?

最佳答案

关于.net - 如何在 .NET 中确定文件的内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/691212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com