错误" URI格式不支持" - 如何拉动图像关闭网站 [英] Error "URI formats are not supported" - How to pull image off web

查看:199
本文介绍了错误" URI格式不支持" - 如何拉动图像关闭网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近写了一张code是这样的:

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

据径处理正确的,但它没有工作,因为它脱下C://开车,所以我改变了图像的位置: http://www.companysite.com/img / XL / 代替。当我把字符串PathToFolder =C:// LiveSite / IMG / XL /; http://www.companysite.com/img / XL / 我得到的错误不支持的URI格式

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"

于是我改变了我的code到:

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

现在我得到一个错误使用.GetFiles - 什么是我想用它代替的GetFiles?是我的code正确的从网站拉的形象呢?任何帮助将是AP preciated。

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.

推荐答案

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

You need to use the webclient class 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();
    }
}
}

code,从借来的:

code borrowed from:

HTTP:// WWW。 fryan0911.com/2009/03/c-download-file-from-website-using-web.html

这篇关于错误" URI格式不支持" - 如何拉动图像关闭网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆