c# - Visualizzazione del contenuto dell'annuncio da Respose.WriteFile () / Response.ContentType

original title: "c# - Displaying ad content from Respose.WriteFile()/ Response.ContentType"


How would one display any add content from a "dynamic" aspx page? Currently I am working on using the System.Web.HttpResponse "Page.Response" to write a file that is stored on a web server to a web request.

This would allow people to hit a url to the type http://www.foo.com?Image=test.jpg and have the image display in their browser. So as you may know this revolves around the use of Response.ContentType.

By using

Response.ContentType = "application/octet-stream";

I am able to display images of type gif/jpeg/png (all i have tested so far), bit trying to display .swf or .ico files gives me a nice little error.


Response.ContentType = "application/x-shockwave-flash";

I can get flash files to play, but then the images are messed.

So how do i easily choose the contenttype?

Come si visualizzerebbe qualsiasi contenuto aggiunto da una pagina aspx "dinamica"? Attualmente sto lavorando sull'utilizzo di System.Web.HttpResponse "Page.Response" per scrivere un file archiviato su un server Web in un we ...

Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"

Tutte le risposte
  • Translate

    This is ugly, but the best way is to look at the file and set the content type as appropriate:

    switch ( fileExtension )
        case "pdf": Response.ContentType = "application/pdf"; break; 
        case "swf": Response.ContentType = "application/x-shockwave-flash"; break; 
        case "gif": Response.ContentType = "image/gif"; break; 
        case "jpeg": Response.ContentType = "image/jpg"; break; 
        case "jpg": Response.ContentType = "image/jpg"; break; 
        case "png": Response.ContentType = "image/png"; break; 
        case "mp4": Response.ContentType = "video/mp4"; break; 
        case "mpeg": Response.ContentType = "video/mpeg"; break; 
        case "mov": Response.ContentType = "video/quicktime"; break; 
        case "wmv":
        case "avi": Response.ContentType = "video/x-ms-wmv"; break; 
        //and so on          
        default: Response.ContentType = "application/octet-stream"; break; 

  • Translate

    This is part of a solution I use on a local intranet. Some of the variables you will have to collect yourself as I pull them from a database but you may pull them from somewhere else.

    The only extra but I've got in there is a function called getMimeType which connects to the database and pulls back the correct mine type based on file extension. This defaults to application/octet-stream if none is found.

    // Clear the response buffer incase there is anything already in it.
    Response.Buffer = true;
    // Read the original file from disk
    FileStream myFileStream = new FileStream(sPath, FileMode.Open);
    long FileSize = myFileStream.Length;
    byte[] Buffer = new byte[(int)FileSize];
    myFileStream.Read(Buffer, 0, (int)FileSize);
    // Tell the browse stuff about the file
    Response.AddHeader("Content-Length", FileSize.ToString());
    Response.AddHeader("Content-Disposition", "inline; filename=" + sFilename.Replace(" ","_"));
    Response.ContentType = getMimeType(sExtention, oConnection);
    // Send the data to the browser

  • Translate

    Yup Keith ugly but true. I ended up placing the MIME types that we would use into a database and then pull them out when I was publishing a file. I still can't believe that there is no autoritive list of types out there or that there is no mention of what is available in MSDN.

    I found this site that provided some help.

  • Translate

    Since .Net 4.5 one can use


    It returns the MIME mapping for the specified file name.