Error “URI formats are not supported” - How to pull image off web

∥☆過路亽.° 提交于 2019-12-24 10:57:03

问题


I recently wrote a piece of code like this:

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

It pathed correctly but it didn't work because it was pulling off the c:// drive so I changed the location of the image to: http://www.companysite.com/img/XL/ instead. When I replace string PathToFolder = "C://LiveSite/img/XL/"; with http://www.companysite.com/img/XL/ I get the error "URI formats are not supported"

So then I changed my code to:

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

Now I get an error from using .GetFiles - What am I suppose to use instead of GetFiles? and is my code correct to pull the image from the web? Any help would be appreciated.


回答1:


You need to use the webclient class http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx.

This should help you out:

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

code borrowed from:

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




回答2:


It looks like you are using files on your own server, then you can grab them like this from codebehind/controller.

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



回答3:


I was having the same problem.

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

What I noticed is that it provides me with the file path of the assembly, but with "file:///" at the start of the string.

So I did this:

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

Hope this helps.



来源:https://stackoverflow.com/questions/17238973/error-uri-formats-are-not-supported-how-to-pull-image-off-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!