0

I am using c# MVC . I have to save an Image from website to another server to reduce the load of my website . First of all i just want to know is it possible to do so. I am trying to acheive this but there is an exception like Url format is wrong. Here is my code

public static string UploadFile(string images, string folder)
    {
        try
        {
            string filePath = string.Empty;
            if (!String.IsNullOrEmpty(images))
            {
                string sourceFileName = System.IO.Path.GetFileName(images);
                string destinationFileName = Path.Combine(folder + "/", sourceFileName);
                string path = "http://imwedding.ansitdev.com/" + destinationFileName;//System.Web.HttpContext.Current.Server.MapPath(destinationFileName);
                System.IO.File.Move(System.Web.HttpContext.Current.Server.MapPath(images), path);

            }
            return filePath;
        }
        catch (Exception ex)   
        {
            throw;
        } 
    }
shami sheikh
  • 552
  • 3
  • 13
  • 26
  • Why not just use round-robin DNS? –  Apr 09 '18 at 13:08
  • can you please share any link ? – shami sheikh Apr 09 '18 at 13:09
  • Is your load really that much? Do you have encounter any errors? Or this is just a brand new requirement? – ngeksyo Apr 09 '18 at 13:12
  • actually it is client's requirement. – shami sheikh Apr 09 '18 at 13:12
  • You can't use `System.IO.File` to copy files over HTTP - that is for local file systems only. For HTTP calls that you need to use something that understands HTTP, like [HttpClient](https://blogs.msdn.microsoft.com/henrikn/2012/02/17/httpclient-downloading-to-a-local-file/) – trailmax Apr 09 '18 at 13:17
  • I don't think File.Move will accept the destination folders as `http://server...`. They are designed to work with only file systems. – Thangadurai Apr 09 '18 at 13:17
  • Possible duplicate of [Download file with WebClient or HttpClient?](https://stackoverflow.com/questions/45711428/download-file-with-webclient-or-httpclient) – trailmax Apr 09 '18 at 13:17
  • If both the servers are on the same network, you can share a folder and use the UNC path convention to save the file in that location, or else you will have to create a simple API to upload file and host it on the other server. – Thangadurai Apr 09 '18 at 13:20
  • Hi All, here is the link to save a file from one server to another in a detailed and step by step way. http://sandblogaspnet.blogspot.in/2009/08/saving-files-from-one-website-to.html – shami sheikh Apr 10 '18 at 08:30

1 Answers1

1

You are trying to copy a file from server A using a physical path to Server B using a virtual path. This can't possibly work like this. The method you are trying to use works across partitions or even disks not across different web servers.

You have two options:

  1. You use a mapped disk, so you map a folder on your second server to a disk on the first and you can then physically copy files from one server to another. You will need the 2 servers to be on the same network for this to work.

  2. You create an upload method on the second server. Create an endpoint located in the second website with an Upload method. There are lots of examples how to do that.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • step 2 is easy to follow. and here is the complete solution how to save a file from one server to another. http://sandblogaspnet.blogspot.in/2009/08/saving-files-from-one-website-to.html – shami sheikh Apr 10 '18 at 08:32