views:

1151

answers:

5

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?

+1  A: 

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

ck
A: 

Have you looked at the HttpResponse.WriteFile method?

Babak Naffas
+1  A: 

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();
Sauron
Using Response.TransmitFile is more efficient than WriteFile, because it does not require the raw data to be buffered.
Josh Stodola
A: 

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

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

Sanghoon
+1  A: 

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>
Kobi