sergserg
sergserg

Reputation: 22224

Save image from the web into my Public/images/items folder?

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

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.

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'."}

So the path is obviously wrong.

Any suggestions?

Upvotes: 0

Views: 896

Answers (1)

Brian
Brian

Reputation: 38025

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

Try something like this instead...

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

Upvotes: 1

Related Questions