C#WinForm网络文件下载问题
具体代码如下:privatevoidLoadFiles(){//stringfromPath="http://www.blzwow.cn/mp3/slfzl.wma";/...
具体代码如下:
private void LoadFiles()
{
//string fromPath = "http://www.blzwow.cn/mp3/slfzl.wma"; //源文件路径
string fromPath = "http://tv.hxspace.cn/mp3/青花瓷.wma";
//string downPath = @"F:\"; //目标路径
string downPath = (@"F:\青花瓷.wma").ToString();
try
{
//create web request/response
HttpWebResponse Response;
HttpWebRequest Request;
Request = (HttpWebRequest)HttpWebRequest.Create(fromPath);
Request.Headers.Add("Translate: f");
Request.Credentials = CredentialCache.DefaultCredentials;
Response = (HttpWebResponse)Request.GetResponse();
Stream respStream = null;
respStream = Response.GetResponseStream();
int FileSize = int.Parse(Response.ContentLength.ToString());
//int FileSize = int.Parse(System.Convert.ToInt64(Response.Headers["Content-Length"]).ToString());
//Do the Download
byte[] buffer = new byte[FileSize];
FileStream fs = File.Open(downPath, FileMode.Create, FileAccess.Write);
int length;
length = respStream.Read(buffer, 0, FileSize);
long writeLength = length;
while (length != 0)
{
fs.Write(buffer, 0, length);
length = respStream.Read(buffer, 0, FileSize);
writeLength += (long)length;
}
fs.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString(), "Error");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.LoadFiles();
}
本人想下载"青花瓷.wma"这个文件到本机F盘,自己测试了,下载确实是下载下来了..不过文件只有530节..后又换了几个文件,皆为530节..几个文件正常大小都有至少1m以上..所以本人百思不得其解为何下下来的只有530k..还请诸位路过的朋友帮忙看看是不是代码问题.. 展开
private void LoadFiles()
{
//string fromPath = "http://www.blzwow.cn/mp3/slfzl.wma"; //源文件路径
string fromPath = "http://tv.hxspace.cn/mp3/青花瓷.wma";
//string downPath = @"F:\"; //目标路径
string downPath = (@"F:\青花瓷.wma").ToString();
try
{
//create web request/response
HttpWebResponse Response;
HttpWebRequest Request;
Request = (HttpWebRequest)HttpWebRequest.Create(fromPath);
Request.Headers.Add("Translate: f");
Request.Credentials = CredentialCache.DefaultCredentials;
Response = (HttpWebResponse)Request.GetResponse();
Stream respStream = null;
respStream = Response.GetResponseStream();
int FileSize = int.Parse(Response.ContentLength.ToString());
//int FileSize = int.Parse(System.Convert.ToInt64(Response.Headers["Content-Length"]).ToString());
//Do the Download
byte[] buffer = new byte[FileSize];
FileStream fs = File.Open(downPath, FileMode.Create, FileAccess.Write);
int length;
length = respStream.Read(buffer, 0, FileSize);
long writeLength = length;
while (length != 0)
{
fs.Write(buffer, 0, length);
length = respStream.Read(buffer, 0, FileSize);
writeLength += (long)length;
}
fs.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString(), "Error");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.LoadFiles();
}
本人想下载"青花瓷.wma"这个文件到本机F盘,自己测试了,下载确实是下载下来了..不过文件只有530节..后又换了几个文件,皆为530节..几个文件正常大小都有至少1m以上..所以本人百思不得其解为何下下来的只有530k..还请诸位路过的朋友帮忙看看是不是代码问题.. 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询