【问题标题】:Error "URI formats are not supported" - How to pull image off web错误“不支持 URI 格式”-如何从 Web 中提取图像
【发布时间】:2013-06-21 15:27:58
【问题描述】:

我最近写了一段这样的代码:

protected void OpenImg_Click(object sender, EventArgs e)
            {
                int i = 0;

                string PathToFolder = "C://LiveSite/img/XL/";

                var dirInfo = new DirectoryInfo(PathToFolder);
                string FileName = Variables.param + "XL.jpg";
                var foundFiles = dirInfo.GetFiles(FileName);

                if (foundFiles.Length == 1)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + PathToFolder + foundFiles[i].Name + "');", true);
                }
            }
        }
   }

它的路径正确,但它不起作用,因为它正在拉出 c:// 驱动器,所以我将图像的位置更改为:http://www.companysite.com/img/XL/。当我将 string PathToFolder = "C://LiveSite/img/XL/"; 替换为 http://www.companysite.com/img/XL/ 时,出现错误“不支持 URI 格式”

然后我将代码更改为:

 protected void OpenImg_Click(object sender, EventArgs e)
                {
                    int i = 0;

                    string uriPath = "http://www.companysite.com/img/XL/";

                    string  localPath = new Uri(uriPath).LocalPath;
                    string FileName = Variables.param + "XL.jpg";
                    var foundFiles = localPath.GetFiles(FileName); //ERROR

                    if (foundFiles.Length == 1)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + uriPath + foundFiles[i].Name + "');", true);
                    }
                }
            }
        }

现在我在使用 .GetFiles 时遇到错误 - 我应该使用什么来代替 GetFiles?我的代码从网络上提取图像是否正确?任何帮助将不胜感激。

【问题讨论】:

    标签: c# .net url uri getfiles


    【解决方案1】:

    您需要使用 webclient 类http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx。

    这应该可以帮助你:

    using System;
    using System.Net;
    using Myapp.Properties;
    
    namespace MyApp
    {
        public class Test
        {
            static void Main(string[] args)
            {
            string website ="http://www.fryan0911.com/webclientsample.zip";
            string downloadfolder = Settings.Default.DownloadFolder;
    
            try
                {
                WebClient wc = new WebClient();
                wc.DownloadFile(website, downloadfolder);
                Console.WriteLine("Web file has been downloaded to " + downloadfolder);
                }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
    }
    

    代码借用自:

    http://www.fryan0911.com/2009/03/c-download-file-from-website-using-web.html

    【讨论】:

      【解决方案2】:

      看起来您正在使用自己服务器上的文件,然后您可以像这样从代码隐藏/控制器中获取它们。

              var serverRootPath = Server.MapPath("~");
              var files = Path.Combine(serverRootPath,"img");
              var images = Directory.GetFiles(files);
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。

        string app_path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
        string folderpath = Path.GetDirectoryName(app_path);// Error Here
        

        我注意到它为我提供了程序集的文件路径,但在字符串的开头带有“file:///”。

        所以我这样做了:

        string app_path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(@"file:///", "");
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2016-05-08
          • 1970-01-01
          • 1970-01-01
          • 2016-07-28
          • 1970-01-01
          • 2013-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多