4


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.

5
  • Your code works just fine for me. Are you getting any further information in the Exception? Any InnerException? Commented Jul 14, 2011 at 12:22
  • @David I get above exception when I download from remote url. Commented Jul 14, 2011 at 14:52
  • I get actual problem and solved it how it getting problem.. I was added extension into file using Path.GetExtension then it was breaked. Commented Jul 15, 2011 at 7:21
  • what is namespace for Server.MapPath ? Commented Jun 24, 2015 at 6:15
  • it says the name Server does not exists in the current context. Commented Jun 24, 2015 at 6:26

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So how do you "I avoid the ".cms" extension from the remoteImageURL" ??
1

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>

1 Comment

Thanks @Yener, actually I solved it my self.. there was issue in extension.. when i was saved file with extension... Thanks Abhishek
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.