Fadi Khalil
Fadi Khalil

Reputation: 291

Copy image from server to my server using its URL C#

I'm trying to copy image from onther server to my server using image url. and here is my code

 internal Boolean changePhoto(int userID, string imagePath)
    {
        Boolean success = true;

        try
        {
            string fileName = Convert.ToString(userID) + ".jpg";

            var currentApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
            currentApplicationPath += "/Images/profiles/";

            var fullFilePath = imagePath;


            // Get the destination path
            var copyToPath = currentApplicationPath + fileName;

            // Copy the file
            System.IO.File.Copy(fullFilePath, copyToPath,true);
        }
        catch (Exception ex)
        {
            DAO.exDao myDao = new DAO.exDao();
            myDao.insert(ex);
            success = false;
        }

        return success;

    }

But i have an exception which is

URI formats are not supported.

The previous method take the user id that will be the file name and take the source of the image that will copy to my server, and when i call it the exception appear.

Upvotes: 1

Views: 1850

Answers (1)

AgentFire
AgentFire

Reputation: 9780

File.Copy inclines you that you cannot use web urls to copy files.

However, there is another approach:

using (WebClient wc = new WebClient())
{
    wc.DownloadFile(fullFilePath, copyToPath);
}

That will download it. However, it does not support local files.

Upvotes: 3

Related Questions