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

18,002

Solution 1

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.

Solution 2

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>
Share:
18,002
Abhishek B.
Author by

Abhishek B.

Currently working as a project management at SSC IT services, India We are working on C#, VB.net,.Net Core, MVC, Angular, SQL server, JQuery, Javascript Microsoft Technology. @abhishekbhalani on twitter Google profile

Updated on June 22, 2022

Comments

  • Abhishek B.
    Abhishek B. almost 2 years


    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.GetExtension(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.

  • Abhishek B.
    Abhishek B. almost 13 years
    Thanks @Yener, actually I solved it my self.. there was issue in extension.. when i was saved file with extension... Thanks Abhishek
  • Monir Tarabishi
    Monir Tarabishi over 9 years
    So how do you "I avoid the ".cms" extension from the remoteImageURL" ??