8

I would like to use standard ASP.NET file download response, like in other Stack Overflow question.

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment; filename=logfile.txt");
Response.TransmitFile( Server.MapPath("~/logfile.txt") );
Response.End();

But inside update panel it does not work. What I have to do, that I will get a file if the download event is triggered inside update panel?

Community
  • 1
  • 1
Peter Stegnar
  • 12,615
  • 12
  • 62
  • 80
  • I'm curious as to why you don't just construct a URL pointing to the file you want to offer to the client, and give that to the browser to use to download your file. ? – 7wp Aug 26 '09 at 07:08
  • It is a txt file, which normally would be just shown directly in the browser. If the content type is set to "application/octet-stream", I guess the browser will initiate a download to disk instead. – awe Aug 26 '09 at 07:59
  • Well "txt" file is just an example. And it is not directly displayed in the browser. With this response it is always downloadable. – Peter Stegnar Aug 27 '09 at 10:26

4 Answers4

10

Well, I have found nice blog post on Encosia which describe solution of this ASP.NET AJAX file download problem. It work really nice.

http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/

Peter Stegnar
  • 12,615
  • 12
  • 62
  • 80
2

You need to have this in a separate aspx that does not use ajax. Ajax is updating existing html markup on a page at the client side. What you try here is replace the responce content on the server side before sending anything to the client.

You could try this:

Have a page called Download.aspx that contains the transmit code you already have.

In your original page, you have a javascript call that calls the download page like this:

window.location.replace('Download.aspx');
awe
  • 21,938
  • 6
  • 78
  • 91
0

You can try making a handler for this work.It is more secure if you can modify well. For this work,you need to encrypt file path in your page where you put a link for the file.

<a href=\"Downloads.ashx?f={0}\" target=\"_blank\">Your link to file</a> 
//{0} -> Encrypted file path
//target = _blank force browser to download file in another window

There are lots of encryption techniques in here

In your Handler page, you need to decrypt the file path into original one so you can read it with System.IO libraries.

context.Response.ContentType = ""; //-->MimeType for your file's extension

You can specify your MimeType by Registry unless your mime-type is static as images.

string mimeType = Registry.GetValue(string.Format(@"HKEY_CLASSES_ROOT\.{0}",
                  Path.GetExtension(decryptedfilePath)), "Content Type", null).ToString();

//Then everything is ready for download

byte[] buffer = File.ReadAllBytes(decryptedfilePath);
context.Response.OutputStream.Write(buffer, 0 , buffer.Length);
context.Response.Flush();

Good Luck.

Myra
  • 3,646
  • 3
  • 38
  • 47
0

I was able to solve this by calling a javascript function, which calls __doPostBack with no __EVENTTARGET.

 function GxGridView_Export(exportLink, exportType) {
     var containingGrid = $(exportLink).closest("table .GxGridViewWithSlider");
     __doPostBack('', containingGrid.attr('id') + "###" + exportType);
 }

The server side Grid then parses the __EVENTARGUMENT and renders the export file.

var eventArg = Page.Request.Form["__EVENTARGUMENT"];
if (!string.IsNullOrEmpty(eventArg) && eventArg.Contains("###"))
{
    var eventParams = eventArg.Split(new string[] { "###" }, StringSplitOptions.RemoveEmptyEntries);
    if (eventParams.Length == 2 && eventParams[0] == this.ClientID)
    {
        ExportGrid(eventParams[1]);
        return;
    }
}
Dale K
  • 491
  • 4
  • 9
  • I initially tried to pass the grid id as the event target, but doPostBack was somehow smart enough to know that's a control inside an ajax panel, and make and ajax postback instead. – Dale K Oct 24 '13 at 18:24