在ASP> NET中下载文件 [英] File Download in ASP>NET

查看:73
本文介绍了在ASP> NET中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试使以下解决方案起作用:

用户来到aspx页面并插入dokument ID在文本框中。然后,此ID用于从数据库生成txt文件。应该提示用户下载文件。



我用简单的回发实现了这个,但是当我尝试在WebMethod上使用ajax时,它没有弹出用户下载文件。换句话说 - 以下不再有效:



Hello,

I am trying to make the following solution to work:
User comes to the aspx page and inserts dokument ID in textbox. Then this ID is used to generate txt file from database. User should be prompted to download the file.

I have implemented this with simple postback, but when I tried to use ajax with WebMethod it's not popping up user to download the file. In other words - the following is no longer working:

HttpContext.Current.Response.ContentType = "txt/application";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Txt_document_title.txt");
            HttpContext.Current.Response.OutputStream.Write(Txt_Stream.GetBuffer(), 0, Txt_Stream.GetBuffer().Length);





我在文档页面上看到它不能与WebMethod一起使用,但不确定这里最好的做法是什么。此外 - 由于安全原因,该文件不应保存在任何地方,应直接发送给用户。



谢谢!



I read on the documentation page that it wont work with WebMethod, but not really sure what would be the best practice here. Also - the file should not be saved anywhere due to security reasons and should go directly to user.

Thanks!

推荐答案

最好的方法是将用户重定向到另一个页面,您将在页面加载中开始下载,在该页面上,您将显示感谢下载或类似内容。
The best approach is to redirect user to another page, where you will start the download in the page load itself and on that page you will display "Thanks for downloading" or something like that.


这篇关于在ASP> NET中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆