4

I am working on an image gallery, and on the browse image module, I have a download icon. I need it to behave as a download button. When the user clicks on it, they should be presented with save file dialog, or the file should be downloaded using their default browser download set-up.

I tried many ways, but no luck.

  • The download Icons appear in a Modal-popup and hence all code is wrapped inside UpdatePannel

  • These images are of different formats (jpeg, gif, tif, psd)

yoozer8
  • 7,361
  • 7
  • 58
  • 93
foo-baar
  • 1,076
  • 3
  • 17
  • 42

2 Answers2

4

Look at http://www.codeproject.com/Articles/74654/File-Download-in-ASP-NET-and-Tracking-the-Status-o or Best way to stream files in ASP.NET

Community
  • 1
  • 1
Sascha
  • 10,231
  • 4
  • 41
  • 65
  • 3
    `AddHeader("Content-Disposition", "attachment;filename="+ FileName.Name);` is the important part to get a save as dialog ... – Hinek May 10 '12 at 11:50
  • @Sascha This seems to be working the only problem is these icons are inside updatePannel, and hence the postback calls are async, I guess that what stopping the code to execute, but when I put the code in Page_Load it worked fine. – foo-baar May 10 '12 at 13:32
  • Glad to hear you're one step further. Maybe this helps in regard to the ajax download: http://groups.google.com/group/ajax-world/browse_thread/thread/b7abc61c1f91376a/af58d68f0b706ae9?hl=en&pli=1 – Sascha May 10 '12 at 13:41
0

Finally sorted out with :

A) To download a file at Client Location :

public void downloadFile(string fileName, string filePath)
    {
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
        Response.WriteFile(filePath + fileName);
    }

B) Since the function triggering controls(imgDownload, imgDownloadPsd) are wrapped under async call : Add this to Page Load :

protected void Page_Load(object sender, EventArgs e)
{   ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(imgDownload);
    ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(imgDownloadPsd);
}  
foo-baar
  • 1,076
  • 3
  • 17
  • 42