Reputation: 2720
I'm having a problem with some reports in the application I'm doing manutention
I've a button that does a postback to the server and do some information and then get back to the cliente and open a popup to download the report.
private void grid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
...
ClientScript.RegisterClientScriptBlock(this.GetType(), "xxx", "<script>javascript:window.location('xx.aspx?m=x','xxx','width=750,height=350,directories=no,location=no,menubar=no,scrollbars,status=no,toolbar=no,resizable=yes,left=50,top=50');</script>");
}
Then in xxx.aspx I've the code:
Response.ClearContent();
Response.ClearHeaders();
Response.TransmitFile(tempFileName);
Response.Flush();
Response.Close();
File.Delete(tempFileName);
Response.End();
This works fine if IE option Automatic prompting for file downloads is enabled. But by default this is disabled and I need to force the download box to be prompting.
Can I do anything without change a lot of code?
Thanks.
Upvotes: 2
Views: 2013
Reputation: 121
Setting the appropriate headers and using the BinaryWrite method will work...
string filePath = "the path to your file";
byte[] fileData = MethodToReadBinaryContentsOfFile(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = null;
Response.AddHeader("Content-Disposition", "attachment;filename=the name to save the file as");
Response.ContentType = "binary/octet";
Response.BinaryWrite(fileData);
System.IO.File.Delete(filePath);
Response.End();
Upvotes: 0
Reputation: 1944
I've done this in the past, seems to work. I'm guessing you're attempting to transmit a file that exists on the hard drive...
Version1
In your Page_Load event handler. Add this...(example is in VB)
'Prepare page for a file download
Response.ContentType = "application/x-msdownload"
Response.AddHeader("content-disposition", "attachment; filename=thefilename.ext")
Response.WriteFile(pathToFile)
This code was used to return an Excel file to the user.
Version 2 (Code taken from an old project)
In a Button Click event on the server
Dim Results As New DataSet()
Results.DataSetName = "SearchExport"
Dim SearchTable As DataTable
Dim sFileName As String = "SearchExport.xls"
Response.ContentType = "application/x-msexcel"
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", sFileName))
SearchTable = GetTheSearchResults()
SearchTable.TableName = "SearchResults"
Results.Tables.Add(SearchTable)
' This example takes the xml and converts it to HTML using XSLT, replace this with your string of data that should be returned. Or put Response.WriteFile() instead.
Response.Write(ConvertToString(Results.GetXml))
Response.End()
Version 2 of this answer is a copy/paste from another project, it doesn't directly do what you're looking for, but you should be able to fill in the gaps. (ie delete temp file, etc)
Upvotes: 1
Reputation: 52518
This is the design of Internet Explorer. The switch tells IE to not show the dialog box, unless the user right-clicks and selects "Download" from the menu.
There is nothing you can do about it.
Automatic Download Blocking automatically suppresses file download dialog boxes not initiated by the user (such as by clicking the mouse or hitting a key). When a dialog box is blocked, the Information Bar appears at the top of the window. Users can download the blocked content by clicking the Information Bar.
Upvotes: 2
Reputation: 4263
There is a header that will cause the download box to be displayed. Try adding the Content-Disposition header to trigger it:
Content-Disposition: attachment; filename=fname.ext
Upvotes: 1