Showing file in browser and content types

O

Oleg Ogurok

Hi all,

In my ASP.NET application, I have a page that serves files from database to
the clients. Here's the code:
private void Page_Load(object sender, System.EventArgs e)

{

byte[] fileData = GetDataBinary(int fileID);


Response.ContentType = "Application/x-msexcel";

Response.BinaryWrite(fileData);

Response.End();

}



I wonder if there is a simple way to set the ContentType for any type of
file. Since I'm storing the file name in the database as well, I can extract
its extension, e.g. .pdf, .xls, .doc, etc. Is there a method in .NET
framework that maps file extension to the corresponding content type?

Thanks.

-Oleg.
 
O

Oleg Ogurok

Nevermind, I've found a way by querying Registry. Apparenly, all supported
content types are stored there.

RegistryKey keyClassesRoot = Registry.ClassesRoot;

RegistryKey keyExtension =
keyClassesRoot.OpenSubKey(info.Extension.ToLower());

string contentType = (string)keyExtension.GetValue("Content Type");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top