Displaying ad content from Respose.WriteFile()/ Response.ContentType

前端 未结 4 2243
抹茶落季
抹茶落季 2021-02-18 21:48

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

相关标签:
4条回答
  • 2021-02-18 22:16

    Since .Net 4.5 one can use

    MimeMapping.GetMimeMapping
    

    It returns the MIME mapping for the specified file name.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.mimemapping.getmimemapping

    0 讨论(0)
  • 2021-02-18 22:18

    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; 
    }
    
    0 讨论(0)
  • 2021-02-18 22:24

    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.Clear();
    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);
    myFileStream.Close();
    
    // 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
    Response.BinaryWrite(Buffer);
    Response.End();
    
    0 讨论(0)
  • 2021-02-18 22:34

    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.

    0 讨论(0)
提交回复
热议问题