2013-06-21 23 views
1

我最近写了这样一段代码:错误“URI格式不支持” - 如何把图片置于网页

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/。当我与http://www.companysite.com/img/XL/取代string PathToFolder = "C://LiveSite/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?并且是我的代码正确从网上拉图像?任何帮助,将不胜感激。

回答

0

它看起来像你正在使用自己的服务器上的文件,那么你可以从抓住他们这样的:从借来的

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

代码代码隐藏/控制器。

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

我遇到了同样的问题。

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

我注意到的是,它为我提供了组件的文件路径,但是,用“文件:///”的字符串的开始。

所以我这样做:

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

希望这有助于。

相关问题