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

Neo4j Logical Log configuration

logging neo4j logical

I am trying to force neo4j to store the logical logs out of the *.db folder and onto a separate volume. However I don't see any option in the neo4j config files that will allow you to do this. I ...

How to make Matlab 2011b support Visual C++ express 2012 in a 64bit system?

visual-studio matlab c++11

How to make Matlab 2011b to work with Visual C++ 2012? It seems