我使用C#提供的WebClient类,进行下载
可是总是出现这个问题
System.Net.WebException: 在 WebClient 请求期间发生异常。 ---> System.UnauthorizedAccessException: 对路径“c:\windows\system32\inetsrv\20050826103100145408.txt”的访问被拒绝。 at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.Net.WebClient.DownloadFile(String address, String fileName) --- 内部异常堆栈跟踪的结尾 --- at System.Net.WebClient.DownloadFile(String address, String fileName) at UNET1.DowmFile.StartDownload() in j:\unet\unet1\dowmfile.aspx.cs:line 240 以下是代码:
public void StartDownload()
{ string filename=UNET1.file.AdminGetFileAddressName(101);
string filefolder=UNET1.file.AdminGetItemFolderByFileID(101);
string URLAddress="http://localhost/Unet/UNET1/"+filefolder+"\\";
string Dir =" C:\\download\\";
string Path = Dir+'\\'+filename;
try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
Response.Write(exp.ToString());
} //这里根本就不执行try
{
client.DownloadFile(URLAddress,filename);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while(allmybyte>0)
{
int m =
str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
} FileStream fstr = new
FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
}
catch(WebException exp)
{
Response.Write(exp.ToString());
}
}
可是总是出现这个问题
System.Net.WebException: 在 WebClient 请求期间发生异常。 ---> System.UnauthorizedAccessException: 对路径“c:\windows\system32\inetsrv\20050826103100145408.txt”的访问被拒绝。 at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.Net.WebClient.DownloadFile(String address, String fileName) --- 内部异常堆栈跟踪的结尾 --- at System.Net.WebClient.DownloadFile(String address, String fileName) at UNET1.DowmFile.StartDownload() in j:\unet\unet1\dowmfile.aspx.cs:line 240 以下是代码:
public void StartDownload()
{ string filename=UNET1.file.AdminGetFileAddressName(101);
string filefolder=UNET1.file.AdminGetItemFolderByFileID(101);
string URLAddress="http://localhost/Unet/UNET1/"+filefolder+"\\";
string Dir =" C:\\download\\";
string Path = Dir+'\\'+filename;
try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
Response.Write(exp.ToString());
} //这里根本就不执行try
{
client.DownloadFile(URLAddress,filename);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while(allmybyte>0)
{
int m =
str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
} FileStream fstr = new
FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
}
catch(WebException exp)
{
Response.Write(exp.ToString());
}
}
我是一只小菜鸟,能不能讲的清楚一点呢!
要不要配置IIS呢!
谢谢
或者你可以用这个代码试一下看看
if(!System.IO.File.Exists(strFile))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
FileInfo fi=new FileInfo(strFile);
Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fi.Name)) ;
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tmpbyte=new byte[1024*8];
FileStream fs=fi.OpenRead();
int count;
while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();
写流文件的形式好象不支持URI下载的吧!!!
还有需要访问c:\windows\system32\inetsrv\20050826103100145408.txt这个么 ?需要的话赋予了相应的权限么?
c:\windows\system32\inetsrv\20050826103100145408.txt
这个就更莫名奇妙了!
我没有用这个路径呀!
我加了断点调试了一下
就是client.DownloadFile(URLAddress,filename);这句话不回执行
是不是URLAddress的问题呢?/
郁闷!!
是不是对应着我服务器上的配置虚拟目录时的文件夹呀!
谢谢
都什么方法支持URL下载呀!!求求你们了!立刻节贴!!!!