如何将图片从网站保存到另一台服务器

How to save an image from website to another server

我正在使用 c# MVC。我必须将图像从网站保存到另一台服务器以减少我网站的负载。首先,我只想知道是否有可能这样做。 我正在尝试实现这一点,但有一个例外,如 Url 格式错误。 这是我的代码

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;
        } 
    }

您正在尝试使用物理路径将文件从服务器 A 复制到使用虚拟路径的服务器 B。这不可能像这样工作。 您尝试使用的方法适用于跨分区甚至磁盘而不是跨不同的 Web 服务器。

您有两个选择:

  1. 您使用映射磁盘,因此您将第二台服务器上的文件夹映射到第一台服务器上的磁盘,然后您可以将文件从一台服务器物理复制到另一台服务器。您需要将 2 台服务器置于同一网络上才能正常工作。

  2. 您在第二台服务器上创建上传方法。使用上传方法创建位于第二个网站的端点。有很多示例如何做到这一点。