做了一个文件下载的程序,共有两个webform,其中一个(命名为default.aspx)是显示要下载的文件名称(hyperlink控件),hyperlink的 NavigateUrl是 "default2.aspx?path=" + fileName;fileName是要下载文件的文件名称,不包括文件路径,只是文件最后的名称 ;另外一个是default2.aspx,控制文件下载过程,default2.aspx中的Page_Load如下所示:
其中: basePath 存储的是文件绝对路径除去文件最后名称的路径,即到文件夹一级的目录,
filepath 存储的是文件的绝对路径,到最后一级文件名
Request["path"].ToString() 通过参数传递过来,文件最后一级的文件名称
pathHistory :是指文件下载完成后要移动到pathHistory目录下,如果有重名的,就在文件名称后面按照数字+1,
本程序要完成的功能是:文件成功下载后,将文件移动到历史文件夹中,并且将原文件在原目录下删除,
我的问题是:文件成功下载后,能够移动到历史文件夹中,但是原文件删除不了,请问是怎么回事,谢谢!!我研究好几天了,一直没有进展,谢谢!!!! ps:我最后一步Response.Write("<script language=javascript>alert('下载完成,移动成功!');</script>");
也没有弹出窗口!! string basePath = HttpContext.Current.Session["FullPath"].ToString();
string filepath = basePath + "\\" + Request["path"].ToString();
//filepath文件的绝对路径
string pathHistory = basePath +"\\History";
Response.Clear();
string fileName; //存储文件最后一级的文件名称 if (File.Exists(filepath))
{
System.IO.FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
System.IO.FileInfo fileInfo = new FileInfo(filepath); string[] fn = filepath.Split('\\');
fileName = fn[fn.Length - 1]; int ibyteCount = (int)fileInfo.Length;
Response.AppendHeader("Content-Length", ibyteCount.ToString());
Response.AppendHeader("Content-Disposition", "attachment");
Response.AppendHeader("filename", Request["path"].ToString()); //Response.ContentType = "application/octet-stream"; Response.StatusCode = 200;
Response.Flush();
byte[] buffer = new byte[2048]; //开始下载
while (ibyteCount > 0)
{
// System.Threading.Thread.Sleep(5000);
if (Response.IsClientConnected)
{
int bytesRead = fs.Read(buffer, 0, Math.Min(buffer.Length, ibyteCount));
Response.OutputStream.Write(buffer, 0, bytesRead);
Response.Flush();
ibyteCount -= bytesRead;
}
else
{
//不删除
//Response.Write("Cancel");
return;
}
}
string newFileName = Path.GetFileName(filepath);
//判断历史文件夹中是否有重名的文件
if (File.Exists(pathHistory + "\\" + fileName))
{
int i = 1;
string fname = Path.GetFileNameWithoutExtension(fileName); //没有后缀的文件名
string ex = Path.GetExtension(fileName); //文件后缀
while (File.Exists(pathHistory + "\\" + fname + i.ToString() + ex))
{
i++;
}
newFileName = fname + i.ToString() + ex;
}
else
{
newFileName = fileName;
}
File.Copy(filepath, pathHistory + "\\" + newFileName);
File.Delete(filepath); //就是这一步没有删除成功,
Response.Write("<script language=javascript>alert('下载完成,移动成功!');</script>");
}
else
{
Response.Clear();
Response.Write("文件不存在!");
}
其中: basePath 存储的是文件绝对路径除去文件最后名称的路径,即到文件夹一级的目录,
filepath 存储的是文件的绝对路径,到最后一级文件名
Request["path"].ToString() 通过参数传递过来,文件最后一级的文件名称
pathHistory :是指文件下载完成后要移动到pathHistory目录下,如果有重名的,就在文件名称后面按照数字+1,
本程序要完成的功能是:文件成功下载后,将文件移动到历史文件夹中,并且将原文件在原目录下删除,
我的问题是:文件成功下载后,能够移动到历史文件夹中,但是原文件删除不了,请问是怎么回事,谢谢!!我研究好几天了,一直没有进展,谢谢!!!! ps:我最后一步Response.Write("<script language=javascript>alert('下载完成,移动成功!');</script>");
也没有弹出窗口!! string basePath = HttpContext.Current.Session["FullPath"].ToString();
string filepath = basePath + "\\" + Request["path"].ToString();
//filepath文件的绝对路径
string pathHistory = basePath +"\\History";
Response.Clear();
string fileName; //存储文件最后一级的文件名称 if (File.Exists(filepath))
{
System.IO.FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
System.IO.FileInfo fileInfo = new FileInfo(filepath); string[] fn = filepath.Split('\\');
fileName = fn[fn.Length - 1]; int ibyteCount = (int)fileInfo.Length;
Response.AppendHeader("Content-Length", ibyteCount.ToString());
Response.AppendHeader("Content-Disposition", "attachment");
Response.AppendHeader("filename", Request["path"].ToString()); //Response.ContentType = "application/octet-stream"; Response.StatusCode = 200;
Response.Flush();
byte[] buffer = new byte[2048]; //开始下载
while (ibyteCount > 0)
{
// System.Threading.Thread.Sleep(5000);
if (Response.IsClientConnected)
{
int bytesRead = fs.Read(buffer, 0, Math.Min(buffer.Length, ibyteCount));
Response.OutputStream.Write(buffer, 0, bytesRead);
Response.Flush();
ibyteCount -= bytesRead;
}
else
{
//不删除
//Response.Write("Cancel");
return;
}
}
string newFileName = Path.GetFileName(filepath);
//判断历史文件夹中是否有重名的文件
if (File.Exists(pathHistory + "\\" + fileName))
{
int i = 1;
string fname = Path.GetFileNameWithoutExtension(fileName); //没有后缀的文件名
string ex = Path.GetExtension(fileName); //文件后缀
while (File.Exists(pathHistory + "\\" + fname + i.ToString() + ex))
{
i++;
}
newFileName = fname + i.ToString() + ex;
}
else
{
newFileName = fileName;
}
File.Copy(filepath, pathHistory + "\\" + newFileName);
File.Delete(filepath); //就是这一步没有删除成功,
Response.Write("<script language=javascript>alert('下载完成,移动成功!');</script>");
}
else
{
Response.Clear();
Response.Write("文件不存在!");
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货