private void DownFile(string fileName,string fullPath)
{
try
{
this.Page.Response.ContentType = "application/octet-stream";
this.Page.Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=102400;//每100K同时下载数据
byte[]readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
this.Page.Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
this.Response.End();
}
catch(Exception Err)
{
Page.RegisterClientScriptBlock("submitAlert",
string.Format("<script language=javascript>submitAlert('{0}');</script>",Err.Message.Trim()));
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
strFlag = Request.QueryString["flag"];
string Dir = Server.MapPath(".");
Dir += @"\upedFile";
FileInfo fi;
DirectoryInfo dirInfo = new DirectoryInfo(Dir);
bool bFind = false;
foreach(FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
{
string fileName = "";
if(fsi is FileInfo)
{
string DocName="";
fi = (FileInfo)fsi;
//取得文件名
fileName = fi.Name;
int tem = fileName.LastIndexOf(".");
if(tem>0)
{
DocName=fileName.Substring(0,tem);
if(DocName == strFlag)
{
//bFind = false;
DownFile(fileName,Dir);
}
}
else
DocName = fileName;
}
}
大家帮我看看,有什么问题?下载下来的文件都是一样的,说:
<form name="Form1" method="post" action="ChangeRepair.aspx?flag=9" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDw4NTkyMDYyMDA7Oz7ht/K8p465MfsG7fgOY+8kffGEgw==" />
<script language=javascript>submitAlert('对路径“B:\chengxu\wxgl\upedFile”的访问被拒绝。');</script>急急急急!!!
{
try
{
this.Page.Response.ContentType = "application/octet-stream";
this.Page.Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=102400;//每100K同时下载数据
byte[]readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
this.Page.Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
this.Response.End();
}
catch(Exception Err)
{
Page.RegisterClientScriptBlock("submitAlert",
string.Format("<script language=javascript>submitAlert('{0}');</script>",Err.Message.Trim()));
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
strFlag = Request.QueryString["flag"];
string Dir = Server.MapPath(".");
Dir += @"\upedFile";
FileInfo fi;
DirectoryInfo dirInfo = new DirectoryInfo(Dir);
bool bFind = false;
foreach(FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
{
string fileName = "";
if(fsi is FileInfo)
{
string DocName="";
fi = (FileInfo)fsi;
//取得文件名
fileName = fi.Name;
int tem = fileName.LastIndexOf(".");
if(tem>0)
{
DocName=fileName.Substring(0,tem);
if(DocName == strFlag)
{
//bFind = false;
DownFile(fileName,Dir);
}
}
else
DocName = fileName;
}
}
大家帮我看看,有什么问题?下载下来的文件都是一样的,说:
<form name="Form1" method="post" action="ChangeRepair.aspx?flag=9" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDw4NTkyMDYyMDA7Oz7ht/K8p465MfsG7fgOY+8kffGEgw==" />
<script language=javascript>submitAlert('对路径“B:\chengxu\wxgl\upedFile”的访问被拒绝。');</script>急急急急!!!
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
后出错而这个时候转入catch,所以你提示地出错信息就放到下载文件中去了