Download images from remote to our server

anu menon asked on May 21, 2015 06:30

Hi

I am working on API Handler which returns results in XML. It contains images and inorder to display it in our website, I cannot use the whole URL since it cannot be accessed globally. It works only in their server machine.So the next option is to download the image to our server and then display it. But is it possible since the url cannot be accessed globally. Also i tried a test code but its not happening

string remoteImageUrl = "http://rack.0.mshcdn.com/media/ZgkyMDE1LzA1LzE4L2JkL01hcmtaVWNrZXJiLjQyNDZjLmpwZwpwCXRodW1iCTM2NHgxMzAjCmUJanBn/4ecf9ce8/877/Mark-ZUckerberg.jpg"; string strRealname = Path.GetFileName(remoteImageUrl); string exts = Path.GetExtension(remoteImageUrl); // Session["mahatta"] = ""; WebClient webClient = new WebClient(); webClient.DownloadFile(remoteImageUrl, HttpContext.Current.Server.MapPath("~/SMD2/media/Content_Images/") + strRealname); image_testing.ImageUrl = "images/" + strRealname;

    Is it because of some path issue???

    Can you please help me

Recent Answers


anu menon answered on May 21, 2015 07:11

I am getting the below error

System.Net.WebException: The remote server returned an error: (403) Forbidden. at System.Net.WebClient.DownloadFile(Uri address, String fileName)

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 21, 2015 13:17

If you have 2 servers and you're trying to download images from one to the other, you'd need the full URL of that image from server1 to store/save it on server2. If your code is on server2, the same would be true, you'd need the full URL of the image/file on server1 to get that image.

So a few questions back to you:

  • where is this code being executed?
  • can you access those images without issue if you manually type in an address?
  • what is your XML returning?
0 votesVote for this answer Mark as a Correct answer

anu menon answered on May 21, 2015 14:31

Actually Now i tried with simple download file code

string remoteImageUrl = "http://rack.0.mshcdn.com/media/ZgkyMDE1LzA1LzE4L2JkL01hcmtaVWNrZXJiLjQyNDZjLmpwZwpwCXRodW1iCTM2NHgxMzAjCmUJanBn/4ecf9ce8/877/Mark-ZUckerberg.jpg"; string strRealname = Path.GetFileName(remoteImageUrl); string exts = Path.GetExtension(remoteImageUrl); // Session["mahatta"] = ""; WebClient webClient = new WebClient(); webClient.DownloadFile(remoteImageUrl, HttpContext.Current.Server.MapPath("~/images/") + strRealname); image_testing.ImageUrl = "images/" + strRealname;

    but its not working. Its showing its been shared by another process or time out error
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.