开发者

How to save image from remote URL using C# asp.net?

开发者 https://www.devze.com 2023-03-20 12:08 出处:网络
I am working on asp.net C# website in that I getting problem when I try to save image from remote URL.

I am working on asp.net C# website in that I getting problem when I try to save image from remote URL.

I have tried with below c# code ...

// C# code

string remoteImageUrl= "http://www.bitpixels.com/getthumbnail?code=83306&url=http://live.indiatimes.com/default.cms?timesnow=1&size=200";
string strRealname = Path.GetFileName(remoteImageUrl);
string exts=Path.GetE开发者_高级运维xtension(remoteImageUrl);

WebClient webClient = new WebClient();
webClient.DownloadFile(remoteImageUrl,Server.MapPath("~/upload/")+strRealname + exts);

When I fetch image from above remoteImageUrl then

I getting error "An exception occurred during a WebClient request."

How can I fetch and save remote url image and store it my website upload directory.

or any other way to get remote url image.


I solved that problem The exception comes due to the extension..

When I getting extension of the remoteImageUrl Path.

string exts = Path.GetExtension(remoteImageUrl);
string strRealname = Path.GetFileName(remoteImageUrl);

It returns ".cms" so exception throws at that point, I avoid the ".cms" extension from the remoteImageURL and then call

WebClient webClient = new WebClient();
webClient.DownloadFile(remoteImageUrl,Server.MapPath("~/upload/")+strRealname + exts);

It works fine.


Yout code is just fine. Make sure your application pool identity, has access to "upload" folder with write access.

And if you are using a proxy server you should also specify this in web.config file.

<system.net>
  <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>


Are you running under anything less than full trust? If so, you can't make arbitrary web requests either.

0

精彩评论

暂无评论...
验证码 换一张
取 消