【发布时间】:2009-03-27 19:38:30
【问题描述】:
我的 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() 需要一个内容类型字符串。我如何知道我的文件的正确内容类型?
【问题讨论】:
-
页面内部如何使用Display功能?
-
没有页面。那是一个 ASP.NET MVC 控制器类。显示是其视图之一。但是,因为这个 View 返回的是 FileStreamResult 而不是 ViewResult,所以它没有 ASPX 页面。
标签: .net file file-io content-type mime-types