现在正用的,起初为了省事,直接用了<a href="down/test.rar">下载</a>..后来又让加个判断,只有登录用户才能下载,本可以在页面通过<% if(--) { %>--等语句实现..上面觉得不合适,那就改成流下载,搜了下,试了下;.
原文地址:http://hi.baidu.com/%C8%FC%C4%C9%D0%D0%D0%C7/blog..也不是原文地址,他也转载的--
注意点:
1. 根据传入的地址,要获取文件名,可以用类似
int intStart = filePathTemp.LastIndexOf("//") + 2;
saveFileName = filePathTemp.Substring(intStart, filePathTemp.Length - intStart);
2.根据传入的地址,获取文件的物理路径,类似
3.主要是利用Response,基本过程 设置类型->添加头部->写入 ->关闭
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(saveFileName));
Response.WriteFile(fileInfo.FullName);//文件流实现
//Response.BinaryWrite(bytes);//字节流实现
// Response.OutputStream.Write(buffer, 0, lengthRead);//分段写入
//Response.TransmitFile(filePathTemp);//TransmitFile写入
Response.End(); //关闭
4.保存文件名,是汉字时容易出现乱码:,用类似
Response.AddHeader( "Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(saveFileName, System.Text.Encoding.UTF8));
5. 疑问:
<1>设置为不弹出保存框,没看到效果
<2>当弹出迅雷下载框时,下载的确是本页面的内容
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Win32;
public partial class downLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
#region
//asp.net中无弹出页面实现下载
//没能看到效果,,,,,不知道为什么?????
//<summary>
/// 下载文件
/// </summary>
/// <param name="filename">文件物理地址</param>
protected void DownloadFile(string filePathTemp)
{
string saveFileName = "";
int intStart = filePathTemp.LastIndexOf("//") + 2;
saveFileName = filePathTemp.Substring(intStart, filePathTemp.Length - intStart);
//string filePath = Server.MapPath(filePathTemp);//路径
System.IO.FileInfo fi = new System.IO.FileInfo(filePathTemp);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
Microsoft.Win32.RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + saveFileName);
Response.ContentType = filecontenttype;
Response.WriteFile(filePathTemp);
Response.Flush();
Response.Close();
Response.End();
}
protected void download_Click(object sender, EventArgs e)
{
//文件的物理地址
string src = "down//1.jpg";
this.DownloadFile(src);
}
#endregion
protected void Button1_Click1(object sender, EventArgs e)
{
downLoad4("down//1.jpg");
// downLoad2("down//1.jpg");
//downLoad2("down//boke.xls");
// downLoad2("down//project.rar");
//downLoad3("down//图片横着轮流显示.html");
//this.DownloadFile("down//图片横着轮流显示.html");
}
//TransmitFile实现下载
protected void downLoad1(string filePathTemp)
{
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
代码如下:
*/
string saveFileName = "";//客户端保存的文件名
int intStart = filePathTemp.LastIndexOf("//") + 2;
saveFileName = filePathTemp.Substring(intStart, filePathTemp.Length - intStart);
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(saveFileName));
string filename = Server.MapPath(filePathTemp);
Response.TransmitFile(filePathTemp);
}
//WriteFile实现下载
/// <summary>
/// 基本上可以,但是当弹出迅雷框下载时,下载的是本页面--不知道为什么
/// </summary>
/// <param name="filePathTemp"></param>
protected void downLoad2(string filePathTemp)
{
/*
using System.IO;
*/
//string fileName ="1.jpg";//客户端保存的文件名
try
{
string saveFileName = "";
int intStart = filePathTemp.LastIndexOf("//") + 2;
saveFileName = filePathTemp.Substring(intStart, filePathTemp.Length - intStart);
string filePath = Server.MapPath(filePathTemp);//获取实际路径
FileInfo fileInfo = new FileInfo(filePath);
if (fileInfo.Exists == false)
{
Response.Write("<script>alert('文件不存在');</script>");
return;
}
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(saveFileName));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
//Response.ContentEncoding = System.Text.Encoding.UTF8; 都可以
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
Response.Write("<script>alert('出现错误');</script>");
return;
}
}
//流方式下载
protected void downLoad3(string filePathTemp)
{
string saveFileName = "";
int intStart = filePathTemp.LastIndexOf("//") + 2;
saveFileName = filePathTemp.Substring(intStart, filePathTemp.Length - intStart);
string filePath = Server.MapPath(filePathTemp);//路径
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(saveFileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);//流写入
Response.Flush();
Response.End();
}
//WriteFile分块下载
protected void downLoad4(string filePathTemp)
{
string saveFileName = "";//客户端保存的文件名
int intStart = filePathTemp.LastIndexOf("//") + 2;
saveFileName = filePathTemp.Substring(intStart, filePathTemp.Length - intStart);
string filePath = Server.MapPath(filePathTemp);//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(saveFileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
}
}
需求的变动??还是考虑太差劲,缺乏经验::
1.先是直接放个链接--后又使用流下载--改为只有会员才可以下载..
2.当判断是否登录时::::::先是新建一个页面(如download.aspx),..当点击下载时跳转到新建的页面,并传递下载文件路径作为参数(download.aspx?file=12.jpg)--在download.aspx页面判断是否登录,没有则不提供下载,否则可以下载-----
3.当不能下载时提示,没有登录,,然后呢?
第一:提供登录框引导用户登录或注册,当注册完成后跳转到登陆或注册前下载页面(如msdn下在)..可以把最后的下载页面作为参数放到session中,在登录成功跳转页面,先判断session中的最后页面值是否为空,不为空则跳转到对应页面,为空则跳转到首页??
第二:(上一条嫌麻烦)只在原页面提示:没有登录.其他不在管 ...问题:提示后仍留在下载页download.aspx页面虽然不能下载--解决:用一个隐藏的iframe...把原来的下载链接download.aspx,的targer指向隐藏的iframe:
<tr><td valign ="top">
<table>
<asp:Repeater ID = "repeaterKejian" Runat="server">
<ItemTemplate>
<tr>
<td width="34" align="center" class="k004">
<a target="HideFrame" href='../commonWeb/DownLoadNow.aspx?file=<%# DataBinder.Eval(Container.DataItem, "NewsID") %>' title="点击下载"><img src="../images/disk.png" width="16" height="16"></a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</td></tr>
</table>
<iframe style="display:none" id="HideFrame" name="HideFrame" src=""></iframe>
4.当传递一个下载问价路径时,把路径暴露在了地址栏中,后改为传递id..在下载页面重新查一下下载文件路径,进行下载