2

How can i download a file [without click any button or link or anchor tag ]in page load event, I want to download a file in a page load it self, please helpe me, thank you

Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219
  • Do you mean you want the person browsing to your website to start downloading a file as soon as they browse to your specified page? Sort of like when you type in a downloadable file in your browser URL? – Kiley Naro Aug 29 '11 at 03:56
  • Hi Kiley Naro, when i send a mial to the user whcih contain a link when he clik i want to get his acknowledgement and at the same time he download the file. – Surya sasidhar Aug 29 '11 at 05:03

2 Answers2

1

In your Page_Load method add following lines, set the appropriate content type and replace yourfile.extention with the full name of file that you want to be downloaded.

this.context.Response.ContentType = "application/filetype";
this.context.Response.AddHeader("content-disposition", "attachment;filename=yourfile.extension");
Waqas
  • 6,812
  • 2
  • 33
  • 50
1

Assuming you have the file to be downloaded on the disk and has path for it. Following code will do the trick.

// You should put more appropriate MIME type as per your file time - perhaps based on extension
Response.ContentType = "application/octate-stream"; 
Response.AddHeader("content-disposition", "attachment;filename=[your file name w/o path]");
// Start pushing file to user, IIS will do the streaming.
Response.TransmitFile(filePath);
VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Hi Vinay, when downloaded complete i want to close my application or webform – Surya sasidhar Aug 29 '11 at 07:44
  • @Surya, unfortunately, that's not possible. Due to security implications, browsers does not allow to close any window via script that has not be opened by script. From better user experience perspective, when your user clicks on the link, it should open download page with some friendly message and using java-script, it should redirect the page to actual download url. The download url should use `content-disposition` as `inline` - for files such as pdf, html, doc etc, browsers are likely to open in same window instead of downloading to disk. Once read, user can close the window – VinayC Aug 29 '11 at 08:28