Reputation: 107
I'm developing a button to download a specific report in an excel sheet I'm using Microsoft.Office.Interop.Excel namespace and everything is OK but my question is how to force asking for save as or open file like any other file download?? Now my excel workbook is automatically downloaded to Documents, by this way the client isn't being aware that the file is downloaded. Any help is highly appreciated
Upvotes: 0
Views: 2105
Reputation: 11
You can achieve this by using Content-Disposition header
add the below line of code in your sample
Response.AppendHeader("content-disposition","attachment; filename=" + name);
first check the extension of the file and save it in the variable "name" then use it as shown in the above code.
Upvotes: 1
Reputation: 800
This is part of the information available on http://weblog.west-wind.com/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET that should help you with your problem:
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.
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. 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. For files outside of the virtual path you have to stream into the OutputStream().
Assuming your file does live inside of the folder hierarchy here's 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 up with the filename of SailBig.jpg as the default filename preset.
Credits for the text to the original author of the article.
Upvotes: 1