展开全部
/// <summary>
/// 下载并保存指定Url的文件,返回文件长度
/// </summary>
/// <param name="FileURL"></param>
/// <param name="FileSavePath"></param>
/// <returns></returns>
public long DownloadFile(string FileURL, string FileSavePath)
{
long contentLength = 0L;
HttpWebRequest request = WebRequest.Create(FileURL) as HttpWebRequest;
request.CookieContainer = this.cookie;
request.AllowAutoRedirect = true;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
try
{
contentLength = response.ContentLength;
byte[] buffer = new byte[0x200];
int count = 0;
count = responseStream.Read(buffer, 0, 0x200);
FileStream stream2 = System.IO.File.Create(FileSavePath);
try
{
while (count > 0)
{
stream2.Write(buffer, 0, count);
count = responseStream.Read(buffer, 0, 0x200);
}
return contentLength;
}
finally
{
stream2.Close();
}
}
catch (Exception ex)
{
RwLog.Log("下载文件失败-"+ex.Message);
}
finally
{
response.Close();
responseStream.Close();
}
return contentLength;
}
上传用 HtmlInputFile控件就可以了
/// 下载并保存指定Url的文件,返回文件长度
/// </summary>
/// <param name="FileURL"></param>
/// <param name="FileSavePath"></param>
/// <returns></returns>
public long DownloadFile(string FileURL, string FileSavePath)
{
long contentLength = 0L;
HttpWebRequest request = WebRequest.Create(FileURL) as HttpWebRequest;
request.CookieContainer = this.cookie;
request.AllowAutoRedirect = true;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
try
{
contentLength = response.ContentLength;
byte[] buffer = new byte[0x200];
int count = 0;
count = responseStream.Read(buffer, 0, 0x200);
FileStream stream2 = System.IO.File.Create(FileSavePath);
try
{
while (count > 0)
{
stream2.Write(buffer, 0, count);
count = responseStream.Read(buffer, 0, 0x200);
}
return contentLength;
}
finally
{
stream2.Close();
}
}
catch (Exception ex)
{
RwLog.Log("下载文件失败-"+ex.Message);
}
finally
{
response.Close();
responseStream.Close();
}
return contentLength;
}
上传用 HtmlInputFile控件就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询