Surya sasidhar
Surya sasidhar

Reputation: 30303

Downloading file in Page load event?

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

Upvotes: 2

Views: 3817

Answers (2)

VinayC
VinayC

Reputation: 49185

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);

Upvotes: 1

Waqas
Waqas

Reputation: 6802

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");

Upvotes: 1

Related Questions