<p>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.</p>

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

<p>By using </p>

<pre><code>Response.ContentType = "application/octet-stream";
</code></pre>

<p>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.</p>

<p>using</p>

<pre><code>Response.ContentType = "application/x-shockwave-flash";
</code></pre>

<p>I can get flash files to play, but then the images are messed.</p>

<p>So how do i <strong>easily</strong> choose the contenttype?</p>

[{'answer_id': 3240, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>The only extra but I\'ve got in there is a function called <strong>getMimeType</strong> 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.</p>\n\n<pre><code>// Clear the response buffer incase there is anything already in it.\nResponse.Clear();\nResponse.Buffer = true;\n\n// Read the original file from disk\nFileStream myFileStream = new FileStream(sPath, FileMode.Open);\nlong FileSize = myFileStream.Length;\nbyte[] Buffer = new byte[(int)FileSize];\nmyFileStream.Read(Buffer, 0, (int)FileSize);\nmyFileStream.Close();\n\n// Tell the browse stuff about the file\nResponse.AddHeader("Content-Length", FileSize.ToString());\nResponse.AddHeader("Content-Disposition", "inline; filename=" + sFilename.Replace(" ","_"));\nResponse.ContentType = getMimeType(sExtention, oConnection);\n\n// Send the data to the browser\nResponse.BinaryWrite(Buffer);\nResponse.End();\n</code></pre>\n'}, {'answer_id': 14496, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 4, 'selected': True, 'text': '<p>This is ugly, but the best way is to look at the file and set the content type as appropriate:</p>\n\n<pre><code>switch ( fileExtension )\n{\n    case "pdf": Response.ContentType = "application/pdf"; break; \n    case "swf": Response.ContentType = "application/x-shockwave-flash"; break; \n\n    case "gif": Response.ContentType = "image/gif"; break; \n    case "jpeg": Response.ContentType = "image/jpg"; break; \n    case "jpg": Response.ContentType = "image/jpg"; break; \n    case "png": Response.ContentType = "image/png"; break; \n\n    case "mp4": Response.ContentType = "video/mp4"; break; \n    case "mpeg": Response.ContentType = "video/mpeg"; break; \n    case "mov": Response.ContentType = "video/quicktime"; break; \n    case "wmv":\n    case "avi": Response.ContentType = "video/x-ms-wmv"; break; \n\n    //and so on          \n\n    default: Response.ContentType = "application/octet-stream"; break; \n}\n</code></pre>\n'}, {'answer_id': 17441, 'author': 'FryHard', 'author_id': 231, 'author_profile': 'https://Stackoverflow.com/users/231', 'pm_score': 0, 'selected': False, 'text': '<p>Yup <a href="https://stackoverflow.com/users/905/keith">Keith</a> 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.</p>\n\n<p>I found <a href="https://web.archive.org/web/20080913141333/http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html" rel="nofollow noreferrer">this</a> site that provided some help.</p>\n'}, {'answer_id': 46413068, 'author': 'shurik', 'author_id': 1159723, 'author_profile': 'https://Stackoverflow.com/users/1159723', 'pm_score': 0, 'selected': False, 'text': '<p>Since .Net 4.5 one can use </p>\n\n<pre><code>MimeMapping.GetMimeMapping\n</code></pre>\n\n<p>It returns the MIME mapping for the specified file name.</p>\n\n<p><a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.mimemapping.getmimemapping" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/dotnet/api/system.web.mimemapping.getmimemapping</a></p>\n'}]
['https://Stackoverflow.com/questions/3234', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/231/']