asp.net 如何将服务器的文件发送到客户端
现在做了一个网页程序,程序生成了一个文件,怎样保存到本地呢?我目前使用的方法是在服务器上生成一个临时文件,然后动态产生一个下载链接,用户点那个链接下载
我现在不想用户自己去下载,我希望用户好像已经点了下载按钮一样,可以选择路径保存文件,怎样实现?
[解决办法]
如果文件小
生成文件时,通过IO读取为流文件,提示用户保存
[解决办法]
responsefile
[解决办法]
public class downfile : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.QueryString["file"] != null) { string path = context.Server.MapPath("/uploadfile/" + context.Request.QueryString["file"]); FileInfo fi = new FileInfo(path); if (fi.Exists) { context.Response.Clear(); context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Server.UrlEncode(fi.Name)); context.Response.AddHeader("Content-Length", fi.Length.ToString()); context.Response.ContentType = "application/octet-stream"; context.Response.Filter.Close(); context.Response.WriteFile(fi.FullName); context.Response.End(); } else { context.Response.Status = "404 File Not Found"; context.Response.StatusCode = 404; context.Response.StatusDescription = "File Not Found"; context.Response.Write("File Not Found"); context.Response.End(); } } }