Web: View raw content of a file from the FileName and the FileContent

I'm using ASP MVC, and I want to allow the user to download/view files from my web server.

The files are not located in this web server.

I know the file content (a byte[] array), and also the file name.

I want same behavior that as the Web Broswer. For example, if the mime type is text, I want to see the text, if it's an image, the same, if it's binary, propose it for a download.

What is the best way to do this?

Thanks in advanced.


The answer for images is available here

For other types, you have to determine the MIME type from the file name extension. You can use either the Windows Registry or some well-known hashtable, or also IIS configuration (if running on IIS).

If you plan to use the registry, here is a code that determines the MIME content type for a given extension:

    public static string GetRegistryContentType(string fileName)
        if (fileName == null)
            throw new ArgumentNullException("fileName");

        // determine extension
        string extension = System.IO.Path.GetExtension(fileName);

        string contentType = null;
        using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension))
            if (key != null)
                object ct = key.GetValue("Content Type");
                if (ct != null)
                    contentType = ct as string;
        if (contentType == null)
            contentType = "application/octet-stream"; // default content type
        return contentType;

Need Your Help

How do I create a sqllite3 in-memory database?

c++ c database sqlite in-memory-database

One of the appropriate uses for sqlite3 is "in-memory databases". This sounds like a really useful tool for my C++ applications. Does anyone have an example of how this is done in C or C++? I'm

Is there a performant JSON based DB with a client side implementation?

database json nosql client-side server-side

I found this question about SQL-like query libraries. What I am looking for is a library that allows queries on JSON objects, embedded in a client-side application (browser), as well as from a remote

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.