This post describes how easy it was for me to include a third-party class library into my existing ASP.NET application, to determine the MIME type of BLOB data.
We used to carry a crappy ASP.NET application as part of our software solutions offering. Though we no longer sell it, there remains one customer installation for which we were obliged to support with bug-fixes. One problem that surfaced was file uploads which accepted only image files. To be accurate, the application accepted user file uploads of all kinds, but for some reason, each of these uploaded files, when downloaded became either a jpeg or gif.
Upon inspection, I saw that when the file is uploaded and stored in a BLOB column, the file type information is not stored along with it. Worse still, jpeg and gif file extensions were just about randomly appended on the download page. Yep, WTF.
My task was to modify the program so that each file download was appended with the right file extension according to its MIME type. This way, the user can use pre-installed viewer softwares to download and view the files e.g. using Adobe Acrobat when a PDF link is clicked. Deciding only to change the download page, with inline C# script blocks (no code-behind), I found a C# library (Winista.Mime) which determined MIME types by analyzing binary data. Sweet.
All I needed to do to start using this wonderful library was to download the project, build it and put the resulting dll in the bin folder of my ASP.NET application. Then, I referenced the library from my download ASPX with this import directive:
<%@ Import Namespace="Winista.Mime" %>
And, in the download portion of the code (which happens to be in the
Page_Load method), I added the right file extension to the Content-Disposition header:
...
Response.AddHeader("Content-Disposition",
"attachment; filename=file." + GetExtension(theBlob) + "; " +
"size=" + theBlob.Length.ToString());
...
You'll notice this piece of code makes a call to a method named
GetExtension(). This is the only place where the library is used. Here it is:
private bool GetExtension(byte[] data)
{
MimeTypes mimeTypes =
new MimeTypes(Request.MapPath("mime-types.xml"));
MimeType mimeType =
mimeTypes.GetMimeType(Winista.Mime.SupportUtil.ToSByteArray(data));
return mimeType.Extensions[0];
}
That's it, short and simple. I forgot to mention that the library stores patterns for each MIME type in an XML file called mime-types.xml, which you'll need to put into the same folder.
If you need to add a class library to your ASP.NET application quickly (no recompiling your ASP.NET application), you can follow the steps described above. If the library you're building targets a different version of .NET, just put it in a bin folder in a virtual directory that has been set to the appropriate version of .NET, and which is accessible from the current application. This also means you'll need to replace the current webpage with a new one residing in the target virtual directory.
Also, although not really related, I previously wrote about using client-side scripting to hack in file upload functionality for a closed-source ASP.NET application (another submodule in the software described in this post):
Using unobtrusive JavaScripts to extend an ASP.NET product
Bad way to extend an ASP.NET product - the details
Bad way to extend an ASP.NET product
4 comments:
http://www.netomatix.com does not exist anymore. Do you know where I can get this code?
Hi, I just found a copy of the code here at
http://www.koders.com/csharp/fid5B088BF104B55AE061B9C5A02C0A5099FD5C9958.aspx?s=system#L10
HI Collin,
I got the code and tried to build to create a dll. But I got errors of "The type or namespace name 'MimeType' could not be found (are you missing a using directive or an assembly reference?)"
How to create a dll?
Thanks,
Jay
http://www.netomatix.com does exist. You can download this library from
http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx
On th every bottom of that page you'll see "Try It" section. Unfortunately, it does not recognize many ordinal file types: it does not recognize text file (prepared by Notebook), does not recognize MS Word 2003 DOC file, it recognizes Microsoft's newest DOCX, XLSX, etc. (Office 2007) files as ZIP files. The later is technically correct, but useless: to stream such files from your Web server you would need to specify them as application/vnd.openxmlformats-officedocument..., not as application/zip ...
So, as it is right now, this library is pretty much useless.
Post a Comment