大家好我想问一下如何从网上下载.jpg文件到我创建的“上传”项目文件夹?
我正在尝试将youtube缩略图图片下载到我的“uploads”文件夹。
我的控制器:
var fileName = Path.GetFileName(http://img.youtube.com/vi/RUgd_GDPhYk/1.jpg);
var path = Path.Combine(Server.MapPath("~/uploads/"), fileName);
file.SaveAs(path);
答案 0 :(得分:1)
查看System.Net.WebClient
,这是一个.NET类,允许您通过HTTP发出资源请求。
http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.100).aspx
提供了检查示例。
var client = new System.Net.WebClient();
var uri = "http://img.youtube.com/vi/RUgd_GDPhYk/1.jpg";
// Here, we're just using the same filename as the resource we're after.
// You may wish to change this to include extra stuff because you'll
// inevitably run into a naming clash - especially with stuff like 1.jpg
var targetFilename = Path.GetFileName(uri);
client.DownloadFile(uri,
Path.Combine(Server.MapPath("~/uploads"), targetFilename));