Sauron
Sauron

Reputation: 16903

Download file without popup in ASP.NET

I am downloading a file using the code

btnDownloadTemplate.Attributes.Add( "onClick", "window.open('StudyReport/WordReportTemplate.doc', 'OpenTemplate', 'resizable=no,scrollbars=no,toolbar=no,directories=no,status=no,menubar=no,copyhistory=no');return false;" );

This will show a popup and the download dialog is shown. How can I avoid the popup and only the download dialog is on the screen?

Upvotes: 3

Views: 12209

Answers (5)

Kobi
Kobi

Reputation: 138017

A common trick is to open the link in an <iframe>. This doesn't require JavaScript, and won't open popups or blank tabs. The <iframe> can be very small, so it's almost invisible.

<iframe name="DownloadDummy">
</iframe>

And the link:

<a href="http://example.com/file.csv" target="DownloadDummy">Download File</a>

Upvotes: 2

San
San

Reputation: 1104

Also, you can just use window.location instead of window.open.

var file = 'StudyReport/WordReportTemplate.doc'; window.location = file;

Upvotes: 1

Sauron
Sauron

Reputation: 16903

I got the answer. I remove the Attributes and add the click event and in it.

    string path = Server.MapPath("");
    path = path + @"\StudyReport\WordReportTemplate.doc";
    string name = Path.GetFileName( path );
    Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
    Response.ContentType = "Application/msword";
    Response.WriteFile( path );
    Response.End(); 

Upvotes: 3

Babak Naffas
Babak Naffas

Reputation: 12561

Have you looked at the HttpResponse.WriteFile method?

Upvotes: 0

cjk
cjk

Reputation: 46425

Don't do a Window.Open, just change the URL of the page to be the document.

Upvotes: 2

Related Questions