一、下载文件
string str = Request.QueryString["D_Url"].ToString(); //虚拟文件路径
string str1 = str.Remove(0, str.LastIndexOf("/") + 1);
string filePath=“~/UserFiles/”;
filePath=System.IO.Path.Combine(filePath,str1); //将文件的物理路径和文件名称合到一块组合成完整的完整文件路径
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath(filePath)));
Response.WriteFile(filePath);
Response.End();
打开文件: string str = Request.QueryString["D_Url"].ToString();
string str1 = str.Remove(0, str.LastIndexOf("/") + 1);
//string str2 = Server.MapPath("UserFiles//") + str1; //打开文件
//FileInfo DownloadFile = new FileInfo(str2);
//Response.Clear();
//Response.ClearHeaders();
//Response.Buffer = false;
//Response.ContentType = "application/octet-stream ";
//Response.AppendHeader("Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
//Response.AppendHeader("Content-Length ", DownloadFile.Length.ToString());
//Response.WriteFile(DownloadFile.FullName);
//Response.Flush();
//Response.End();
文件下载处理
本文介绍了一种使用 ASP.NET 处理文件下载的方法,包括如何构造文件路径、设置响应头以允许用户下载文件,以及如何直接打开文件等内容。通过具体的代码示例展示了整个过程。

被折叠的 条评论
为什么被折叠?



