<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<table style="width: 100%">
<tr>
<td style="width: 10px">
</td>
<td align="left">
a href='<%#Eval("DFILE_URL") %>'
<%# Bll.WebManage.strCut(DataBinder.Eval(Container.DataItem, "ARTICLE_TITLE").ToString(), 20)%></a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>DFILE_URL:文档路径ARTICLE_TITLE:文档名
这样另存为可以,但是点击文件标题打开文档,而不是出现下载对话框。如何解决?
FileInfo DownloadFile = new FileInfo(sfilepath); 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();
<a href="download.ashx?url= <%=Server.UrlEncode("说明.txt")%>">下载 </a>
------------------------------------------------------------------------------
download.ashx<%@ WebHandler Language="C#" Class="download" %>
using System;
using System.Web;
public class download : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
downloadfile(url);
} public bool IsReusable {
get {
return false;
}
}
public void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="fullName">待下载的文件名</param>
/// <param name="fileName">下载后的文件名</param>
/// <param name="encoding">字符编码</param>
public static void DownLoad(string fullName, string fileName, System.Text.Encoding encoding)
{
FileInfo info = new FileInfo(fullName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, encoding));
HttpContext.Current.Response.AppendHeader("Content-Length", info.Length.ToString());
HttpContext.Current.Response.WriteFile(info.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
}
{
System.IO.FileInfo file = new System.IO.FileInfo(FilePath(filePath));
if (file.Exists)
{
FileStream fs = new FileStream(Server.MapPath(filePath), FileMode.Open); // 设置文件流,filePath为文件路径
int length = ((int)fs.Length) > 0 ? (int)fs.Length : 1;
// byte[] bytes = new byte[(int)fs.Length];
byte[] bytes = new byte[length];
fs.Read(bytes, 0, bytes.Length); // 读取
fs.Close();
Response.ClearContent(); // 清楚缓冲区所有内容
Response.ClearHeaders(); // 清楚缓冲区所有头
Response.ContentType = "application/octet-stream"; // 设置输出流的Http MIME类型
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //fileName为需要下载的文件名
Response.BinaryWrite(bytes); // 写入输入流
Response.Flush(); // 向客户端发送数据流
Response.End();
}
else
{
Response.Write("<script type=\"text/javascript\">alert('该附件不存在!')</script>");
}
}