Note thought that Response.TransmitFile() in recent versions of IIS can only serve files out of the virtual folder hierarchy of the Web site or virtual. The optimal way to do this is to use Response.TransmitFile() to explicitly send the file from your ASP.NET application and then add the Content Type and Content-Disposition headers. If you want to force a Save As dialog automatically when a link is clicked via code from the server side, you have to send the file back through code using Response.TransmitFile(), Response.BinaryWrite() or streaming it into the Response.OutputStream and add a couple of custom headers to the output. Chrome - depending on the options - will simply download the file to your Downloads folder without prompting for anything. Internet Explorer shows the yellow bottom bar asking whether you want to save the file. Firefox has this nice dialog that gives you choices. Note that this behavior varies from browser to browser though. Here's what this dialog looks like in FireFox when you specify a Content-Disposition header: You specify that the output is an attachment and the name that is to be displayed on the download dialog (or the actual downloaded file if auto-download is used). Response.TransmitFile(Server.MapPath("~/images/sailbig.jpg")) Response.AppendHeader("Content-Disposition", "attachment filename=SailBig.jpg") In ASP.NET you can add the Content-Disposition header like this: Response.ContentType = "image/jpeg" The Content-Disposition header basically allows you to specify that the response you're sending back is to be treated as an attachment rather than content which results in the download dialog. The key element to allow a file to be treated as a file rather than content to be displayed in the browser is the Content-Disposition header. You can of course always use the browser short cut menu and use the Save Target As option to save the file to disk. So when you click on a link like a jpg image the browser knows it's an image and will display that image. ![]() The Web Server provides a content type based on mime-type mappings, and based on that content type the browser serves the page and displays it. Normally, when you link a file that file will always display inside of the browser because the browser loads it and automatically determines the content type based on the file extension. see the Save As dialog)?" Response.AppendHeader("Content-Disposition", "attachment filename=SailBig.jpg") Here is a common question that I hear frequently: "How do I download a file from a Web site, but instead of displaying it in the browser see it as a file that can be saved (ie.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |