如何在c#中将图像从主域复制到子域文件夹

时间:2014-09-18 06:59:55

标签: c# asp.net

我正在尝试将我的文件从主域复制到子域折叠

我习惯了下面的代码

CopyFiles("http://192.168.10.119:8010/1.jpg", "http://192.168.10.120:8010/TestFolder/1.jpg");

 public void CopyFiles(string sourcePath, string destinationPath)
    {
        string[] files = System.IO.Directory.GetFiles(sourcePath);
        Parallel.ForEach(files, file =>
        {
            System.IO.File.Copy(file, System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(file)));

        });
    }

然后我收到了一个错误:

  

不支持URI格式。

任何人都可以解决这个问题。谢谢:))

3 个答案:

答案 0 :(得分:2)

System.IO.File.Copy无法将文件从一个URL(通过http)移动到另一个URL。您需要指定有效的本地路径,例如

CopyFiles("C:\\BasePath\\1.jpg", "C:\\Target\\TestFolder\\1.jpg");

这意味着您需要在计算机上本地运行带有文件的程序,或者可以通过本地网络访问它们(无需通过互联网)。

如果您想从远程网络服务器网址下载图片,可以在此处找到相关信息copy image from url

写入远程位置变得更加棘手,您基本上需要在远程主机(接收文件的人)上安装FTP服务器才能将文件发送到how to send files to FTP。 Web服务器不接受您发送给他们的任何文件,想象一下,如果有人可以将文件上传到他们想要的任何服务器

答案 1 :(得分:0)

你不需要提供像" 192.168.10.119:8010 / 1.jpg"如, 服务器可以直接访问同一服务器的文件夹, 所以你可以提供像/YourFolderName/fileName.jpg"

这样的路径

希望这会对你有所帮助。

      CopyFiles("/1.jpg", "/TestFolder");



 public void CopyFiles(string sourcePath, string destinationPath)
    {
            string filePath = System.Web.HttpContext.Current.Server.MapPath(destinationPath).ToString();
            sourcePath = System.Web.HttpContext.Current.Server.MapPath(sourcePath);
            System.IO.File.Copy(sourcePath, System.IO.Path.Combine(filePath, System.IO.Path.GetFileName(sourcePath)));
    }

答案 2 :(得分:-2)

请对您的代码进行以下更改。 希望对你有帮助, 此代码将把所有图像从一个地方复制到另一个地方。

功能调用

 CopyFiles("/DomainFolder/Images", "/DomainFolder/SubDomainFolder/Images");

方式

public void CopyFiles(string sourcePath, string destinationPath)
    {
        string[] files = System.IO.Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath(sourcePath));
        foreach (var file in files)
        {
            string filePath = System.Web.HttpContext.Current.Server.MapPath(destinationPath).ToString();

            System.IO.File.Copy(file, System.IO.Path.Combine(filePath, System.IO.Path.GetFileName(file)));

        };
    }

感谢你,

此致 JJRS