如何取得WebClient.DownloadFile(...)时的文件的真正类型,比如是text文本或图片?

大可山人
博客专家认证
2006-01-18 05:52:07
myWebClient.DownloadFile(myStringWebResource, @"c:\tmp\" + fileName);
起因:当我们下载这个链接http://img.star.tfol.com/star/stars/1513/photos/M49159.jpg时,本图片并不存在,于是返回到一个网页中了。这样下载东西保存为M49159.jpg没有一点意义。
但这个http://img.star.tfol.com/star/stars/1513/photos/M49169.jpg就是一个图片,如何下载前或下载时区别下载的类型为文本或图片呢?
...全文
236 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
大可山人 2006-01-24
  • 打赏
  • 举报
回复
UP
大可山人 2006-01-23
  • 打赏
  • 举报
回复
WebClient应该还有对FTP,UDP等的封装吧?不太了解
Ivony 2006-01-20
  • 打赏
  • 举报
回复
WebClient无非就是对HttpWebRequest和HttpWebResponse的封装,你想要这些功能,可以直接用HttpWebRequest和HttpWebResponse哈。
大可山人 2006-01-20
  • 打赏
  • 举报
回复
楼上兄弟:
我说的是下载,不是上传。
回归温柔 2006-01-20
  • 打赏
  • 举报
回复
上传文件的时候把文件的后缀名更改,你可以自己定一套标准 比如.jpg可是.图片
大可山人 2006-01-20
  • 打赏
  • 举报
回复
大可山人 2006-01-18
  • 打赏
  • 举报
回复
我知道使用HttpWebRequest可以区分文本(如下),但WebClient为何没有类似的方法呢?

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
char seperator = '/';
String contenttype = myHttpWebResponse.ContentType;
// Retrieves the text if the content type is of text/html.
String maintype = contenttype.Substring(0,contenttype.IndexOf(seperator));
if (String.Compare(maintype,"text") == 0)
{
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader( receiveStream, encode );
char[] read = new char[256];
int count = readStream.Read( read, 0, 256 );
int index = 0;
while (index < myHttpWebResponse.ContentLength)
{
String str = new String(read, 0, count);
index += count;
count = readStream.Read(read, 0, 256);
}
receiveStream.Close();
}
myHttpWebResponse.Close();

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧