My WPF application gets a file from the user with 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
And my ASP.NET MVC application serves it up for download at a web site with 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
But FileStreamResult() needs a content type string. How do I know the correct content type of my file?
For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.
Commands for displaying file contents (pg, more, page, and cat commands) The pg, more, and page commands allow you to view the contents of a file and control the speed at which your files are displayed. You can also use the cat command to display the contents of one or more files on your screen.
content_type is an alias for mimetype. Historically, this parameter was only called mimetype, but since this is actually the value included in the HTTP Content-Type header, it can also include the character set encoding, which makes it more than just a MIME type specification.
The application/octet-stream MIME type is used for unknown binary files. It preserves the file contents, but requires the receiver to determine file type, for example, from the filename extension. The Internet media type for an arbitrary byte stream is application/octet-stream .
I made a C# helper class based on Zacks response.
/// <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";
}
}
You might, of course, want to keep the mimeTypes
alive for future queries. This is just a starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With