Save Excel In Downloads Folder On Client Pc Asp.Net C# UPDATED

Save Excel In Downloads Folder On Client Pc Asp.Net C#

Hither is a common question that I hear ofttimes: "How do I download a file from a Spider web site, but instead of displaying it in the browser come across information technology equally a file that can be saved (ie. meet the Save Equally dialog)?"

          Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg");                  

Unremarkably, when you link a file that file will always display inside of the browser because the browser loads information technology and automatically determines the content type based on the file extension. The Spider web Server provides a content type based on mime-blazon mappings, and based on that content type the browser serves the page and displays it.

And then when you click on a link similar a jpg image the browser knows it's an epitome and volition display that prototype. You lot tin can of form always use the browser short cut menu and use the Save Target Equally option to save the file to disk.

The Content-Disposition Header

The central 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.

The Content-Disposition header basically allows you to specify that the response you're sending dorsum is to exist treated every bit an attachment rather than content which results in the download dialog.

In ASP.NET you lot can add the Content-Disposition header like this:

          Response.ContentType = "paradigm/jpeg"; Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg"); Response.TransmitFile(Server.MapPath("~/images/sailbig.jpg")); Response.End();                  

Yous 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).

Hither's what this dialog looks like in FireFox when yous specify a Content-Disposition header:

SaveAsDialog

Note that this behavior varies from browser to browser though. Firefox has this nice dialog that gives you choices. Internet Explorer shows the yellowish bottom bar asking whether you want to salve the file. Chrome - depending on the options - volition merely download the file to your Downloads folder without prompting for annihilation.

Sending a Binary File to the Client

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 lawmaking using Response.TransmitFile(), Response.BinaryWrite() or streaming it into the Response.OutputStream and add a couple of custom headers to the output.

The optimal way to do this is to use Response.TransmitFile() to explicitly transport the file from your ASP.NET awarding and and so add the Content Blazon and Content-Disposition headers. Note thought that Response.TransmitFile() in recent versions of IIS can but serve files out of the virtual folder hierarchy of the Spider web site or virtual. For files exterior of the virtual path yous have to stream into the OutputStream().

Bold your file does live inside of the binder bureaucracy hither'south how you can force the Save As dialog for output sent with TransmitFile():

          Response.ContentType = "image/jpeg"; Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg"); Response.TransmitFile(Server.MapPath("~/images/sailbig.jpg")); Response.End();                  

This will cause a Open / Save As dialog box to pop upwardly with the filename of SailBig.jpg as the default filename preset.

Tin can't use TransmitFile?

The previous instance used TransmitFile() which is well-nigh efficient, merely yous don't e'er have files to work with or you may have files that alive exterior of the directory construction of your Spider web site which TransmitFile() doesn't let for. Luckily there are other ways to ship binary data to the client from ASP.Internet code.

To write binary data that is a byte[] yous can use the Response.BinaryWrite() method to write out binary data. If you accept a Stream of binary data, you lot tin stream the data directly into the into the Response.OutputStream.

Hither's an example of streaming an paradigm straight into the Response.OutputStream from a Bitmap:

          Bitmap bmp = ImageUtils.CornerImage(); // retrieve an prototype  Response.ContentType = "image/jpeg"; Response.AppendHeader("Content-Disposition", "attachment; filename=LeftCorner.jpg");  bmp.Save(Response.OutputStream, ImageFormat.Jpeg);                  

This code but uses the bmp.Salve() method which writes a binary stream into the Response.OutputStream which results in the binary information being sent to the client. Whatever Stream features (like Stream.Copy) behaviors tin can be applied to the OutputStream to stream data to the customer.

If you're dealing with files, endeavour to avoid streams if you tin and use TransmitFile() instead. TransmitFile is very efficient because information technology offloads the file streaming to IIS which happens off an I/O port then the file streaming releases the IIS request thread while returning the file. This is especially useful when streaming large files to the client as information technology doesn't tie up the IIS pipeline.

Saving rather than Viewing

Browsers are focused on displaying content within the browser'due south viewport. When you do demand to download content rather than view Content-Disposition is your friend… I hope you found this short postal service useful.

DOWNLOAD HERE

Posted by: polancotakedent.blogspot.com

Post a Comment

0 Comments