0

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

我正在尝试将 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);
4

1 回答 1

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));
于 2013-01-23T16:07:52.997 回答