Download file without popup in ASP.NET

佐手、 提交于 2019-12-11 04:07:01

问题


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:


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



回答2:


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




回答3:


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>



回答4:


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

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




回答5:


Have you looked at the HttpResponse.WriteFile method?



来源:https://stackoverflow.com/questions/986075/download-file-without-popup-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!