2013-01-23 41 views
0

大家好我想问一下如何从网上下载.jpg文件到我创建“上传”的项目文件夹?如何从web下载jpg到MVC3中的项目文件夹?

我想将youtube缩略图下载到我的“上传”文件夹中。

我的控制器:

var fileName = Path.GetFileName(http://img.youtube.com/vi/RUgd_GDPhYk/1.jpg); 
       var path = Path.Combine(Server.MapPath("~/uploads/"), fileName); 
file.SaveAs(path); 

回答

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)); 
+0

非常感谢 – learnmore

相关问题