将图像从Web保存到我的Public / images / items文件夹中?

时间:2022-06-18 06:13:48

I'm trying to save an image from a website and save it to my ~/Public/images/items folder.

我正在尝试从网站保存图像并将其保存到我的〜/ Public / images / items文件夹中。

string localFilename = @"~\Public\images\items\" + item.Name + ".png";
string imageUrl = "https://account.hirezstudios.com/smitegame/" + CQ.Create(itemBox)["#itemIcon a img"].Attr("src");
using (var web = new WebClient())
{
    web.DownloadFile(imageUrl, localFilename);
}

An exception fired because the path in localFilename is not correct.

由于localFilename中的路径不正确而触发了异常。

Here's what the actual variable holds (checked using a breakpoint):

这是实际变量的含义(使用断点检查):

~\Public\images\items\Aegis Amulet.png

And the exception:

例外:

{"No se puede encontrar una parte de la ruta de acceso 'C:\Program Files (x86)\IIS Express\~\Public\images\items\Aegis Amulet.png'."}

{“No se puede encontrar una parte de la ruta de acceso'C:\ Program Files(x86)\ IIS Express \〜\ Public \ images \ items \ Aegis Amulet.png'。”}

So the path is obviously wrong.

所以道路显然是错误的。

Any suggestions?

有什么建议么?

1 个解决方案

#1


1  

WebClient doesn't understand the ~ in a path. You need to have a local full path to where you want to save the file.

WebClient不理解路径中的〜。您需要具有要保存文件的位置的本地完整路径。

Try something like this instead...

尝试这样的事情......

Server.MapPath(@"\Public\images\items\" + item.Name + ".png")

#1


1  

WebClient doesn't understand the ~ in a path. You need to have a local full path to where you want to save the file.

WebClient不理解路径中的〜。您需要具有要保存文件的位置的本地完整路径。

Try something like this instead...

尝试这样的事情......

Server.MapPath(@"\Public\images\items\" + item.Name + ".png")