protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
//Session["userid"] = "adminTest"; //下载文件页面不能用session
string userid = "";
userid = Request.QueryString["uid"].ToString().Trim();
string FileGuid = Request.QueryString["id"].ToString().Trim();
Guid guid = new Guid(FileGuid); string FileServerPath = "";
FileServerPath = bllfile.GetFilePath(userid, guid).ServerPath;
if (FileServerPath == "")
{
Response.Write("<font size=3 color=red>没有找到文件!</font>");
}
else
{
if (!SendFile(FileServerPath))
{
Response.Write("<font size=3 color=red>下载出错,请与管理员联系!</font>"); }
else
{
int k = 0;
k = k + bllfile.Update(guid); //这里是去更新文件下载次数+1,但是文件越大下载的时间越长,这里就会多次执行,为什么,而不是只执行一次
if (k < 1)
{
Response.Write("<font size=3 color=red>更新您的下载次数失败</font>");
}
}
}
catch (Exception ex)
{
Common.LogManager.LogWrite("Inner_ResourceList_DownFile-->Page_Load", ex.ToString());
Response.Write("<font size=3 color=red>对不起,无法下载文件!</font>");
} Response.End();
}
}
private bool SendFile(string filename)
{
bool check;
try
{
Response.Clear();
Response.ClearHeaders();
FileInfo f = new FileInfo(filename); Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(f.Name));
Response.TransmitFile(filename); check = true;
}
catch (Exception ex)
{
check = false;
}
return check;
}
k = k + bllfile.Update(guid); //这里是去更新文件下载次数+1,但是文件越大下载的时间越长,这里就会多次执行,为什么,而不是只执行一次,发现这个page_load里的东西好像是循环碰次执行,导致这一句多次执行。
难道是Response.TransmitFile(filename);导致的向客户发送数据会多次发送?
{
if (!IsPostBack)
{
try
{
//Session["userid"] = "adminTest"; //下载文件页面不能用session
string userid = "";
userid = Request.QueryString["uid"].ToString().Trim();
string FileGuid = Request.QueryString["id"].ToString().Trim();
Guid guid = new Guid(FileGuid); string FileServerPath = "";
FileServerPath = bllfile.GetFilePath(userid, guid).ServerPath;
if (FileServerPath == "")
{
Response.Write("<font size=3 color=red>没有找到文件!</font>");
}
else
{
if (!SendFile(FileServerPath))
{
Response.Write("<font size=3 color=red>下载出错,请与管理员联系!</font>"); }
else
{
int k = 0;
k = k + bllfile.Update(guid); //这里是去更新文件下载次数+1,但是文件越大下载的时间越长,这里就会多次执行,为什么,而不是只执行一次
if (k < 1)
{
Response.Write("<font size=3 color=red>更新您的下载次数失败</font>");
}
}
}
catch (Exception ex)
{
Common.LogManager.LogWrite("Inner_ResourceList_DownFile-->Page_Load", ex.ToString());
Response.Write("<font size=3 color=red>对不起,无法下载文件!</font>");
} Response.End();
}
}
private bool SendFile(string filename)
{
bool check;
try
{
Response.Clear();
Response.ClearHeaders();
FileInfo f = new FileInfo(filename); Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(f.Name));
Response.TransmitFile(filename); check = true;
}
catch (Exception ex)
{
check = false;
}
return check;
}
k = k + bllfile.Update(guid); //这里是去更新文件下载次数+1,但是文件越大下载的时间越长,这里就会多次执行,为什么,而不是只执行一次,发现这个page_load里的东西好像是循环碰次执行,导致这一句多次执行。
难道是Response.TransmitFile(filename);导致的向客户发送数据会多次发送?
Response.ContentType = "application/octet-stream";
Response.WriteFile("");
Response.End();
(1)修改数据库该文件下载次数
(2)Response.Write()发送文件
这两步在同一个页面,
但是我发现,如果文件大,步骤(1)就会多次执行(好像是一直执行,直到Write()结束)。。
如何解决这个问题?
由于要下的文件有超过1G的,所以response.writeFile()不太合适吧
如果有range,就不要+1。
详情请看
http://hi.baidu.com/longhtml/blog/item/d4d96000f984f680e850cd04.html
根据IP来判断. 如果同一个IP在1个小时内下载两次的话. 就算一次. 你下载的文件有上G. 估计他们一天最多也就下两次.
我以前写了个记录文件下载是否成功的功能. .
public class FileInforMation
{
private FileInfo _objFile;
private DownLoadState _downloadState;
public FileInforMation(string path)
{
_objFile = new FileInfo(path);
}
/// <summary>
/// 文件是否存在
/// </summary>
public bool Exists
{
get { return this._objFile.Exists; }
} /// <summary>
/// 文件全名
/// </summary>
public string FullName
{
get { return this._objFile.FullName; }
} public DateTime LastWriteTimeUTC
{
get { return this._objFile.LastWriteTimeUtc; }
} public long Length
{
get { return this._objFile.Length; }
}
public string ContentType
{
get { return "application/x-zip-compressed"; }
} public string EntityTag
{
get { return "MyExampleFileID"; }
} public DownLoadState State
{ get { return this._downloadState; }
set { this._downloadState = value;
Save(value);
}
}
/// <summary>
///
/// </summary>
private void Save(DownLoadState state)
{ FileStream file = File.Create(HttpContext.Current.Server.MapPath("~/"+state+".txt"));
file.Flush();
file.Close();
}
}
/// <summary>
/// 下载状态
/// </summary>
[Flags]
public enum DownLoadState
{
/// <summary>
/// 下载中..
/// </summary>
DownLoading = 1,
/// <summary>
/// 下载中断
/// </summary>
DownloadCancel =2,
/// <summary>
/// 下载成功
/// </summary>
DownloadSucceed = 3
}
public class ZIPHttpHandler : IHttpHandler
{ public void ProcessRequest(HttpContext objContext)
{
HttpResponse objResponse = objContext.Response;
HttpRequest objRequest = objContext.Request;
int iBufferSize = 25000;
byte[] bBuffer = new byte[iBufferSize + 1];
FileInforMation objFile = new FileInforMation(objContext.Server.MapPath("~/download.zip"));
objResponse.Clear(); //判断文件是否存在
if (!objFile.Exists)
{
objResponse.StatusCode = 404;
}
else
{
//设置Http 头 信息
//内容长度 Content-Length:文件长度的字节数
objResponse.AppendHeader("Content-Length", objFile.Length.ToString()); objResponse.AppendHeader("Accept-Ranges", "bytes");
//设置内容的类型
objResponse.ContentType = objFile.ContentType; bool bDownloadBroken = false;
objResponse.Flush();
//设置状态为下载中
objFile.State = DownLoadState.DownLoading;
//创建一个文件流
FileStream objStream = new FileStream(objFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); var iBytesToRead = objStream.Length;
while (iBytesToRead > 0)
{
//判断是否连接中
if (objResponse.IsClientConnected)
{
int iLengthOfReadChunk = objStream.Read(bBuffer, 0, Convert.ToInt32(Math.Min(bBuffer.Length, iBytesToRead)));
//输入文件流每次输出25001个字节
objResponse.OutputStream.Write(bBuffer, 0, iLengthOfReadChunk);
objResponse.Flush();
bBuffer = new byte[iBufferSize + 1];
iBytesToRead -= iLengthOfReadChunk;
}
else
{
iBytesToRead = -1;
bDownloadBroken = true;
break;
}
}
if (bDownloadBroken)
{
//下载中断
objFile.State = DownLoadState.DownloadCancel;
}
else
{
//下载成功
objFile.State = DownLoadState.DownloadSucceed;
}
objStream.Close();
}
objResponse.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
想问一下这个是不是针对zip文件格式的下载代码?
Request.Headers["Range"]可以取Range数据。
如果要支持断点续传,Response.AddHeader("Content-Range","bytes 0-800/801")作为断点续传的响应。