Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不支持URL格式_C#_Asp.net - Fatal编程技术网

C# 不支持URL格式

C# 不支持URL格式,c#,asp.net,C#,Asp.net,我用File.OpenRead方法读取文件,我给出了这个路径 http://localhost:10001/MyFiles/folder/abc.png 我也试过这个,但运气不好 http://localhost:10001//MyFiles//abc.png 但它的给予 不支持URL格式 当我像这样给出驱动器的物理路径时,它工作得很好 d:\MyFolder\MyProject\MyFiles\folder\abc.png 如何将文件路径指定给Http路径 这是我的密码 public

我用File.OpenRead方法读取文件,我给出了这个路径

   http://localhost:10001/MyFiles/folder/abc.png
我也试过这个,但运气不好

http://localhost:10001//MyFiles//abc.png
但它的给予

不支持URL格式

当我像这样给出驱动器的物理路径时,它工作得很好 d:\MyFolder\MyProject\MyFiles\folder\abc.png

如何将文件路径指定给Http路径

这是我的密码

public FileStream GetFile(string filename)
{
    FileStream file = File.OpenRead(filename);
    return file;
}

看看
WebClient
(),它有许多从web下载数据的实用方法

如果要将资源作为
,请尝试:

using(WebClient webClient = new WebClient())
{
    using(Stream stream = webClient.OpenRead(uriString))
    {
        using( StreamReader sr = new StreamReader(stream) )
        {
            Console.WriteLine(sr.ReadToEnd());
        }
    }
}
我找到了这个片段。可能正是您需要的:

using(WebClient client = new WebClient()) {
   string s = client.DownloadFile(new Uri("http://.../abc.png"), filename);
}

它使用类。

您可以按照其他答案中的建议使用WebClient,也可以像下面这样获取相对路径:

var url = "http://localhost:10001/MyFiles/folder/abc.png";

var uri = new Uri(url);
var path = Path.GetFileName(uri.AbsolutePath);

var file = GetFile(path);
// ...

一般来说,您应该去掉绝对URL。

下载HTML的最佳方法是使用WebClient类。您可以这样做:

    private string GetWebsiteHtml(string url)
    {
        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        string result = reader.ReadToEnd();
        stream.Dispose();
        reader.Dispose();
        return result;
    }
然后,如果您想进一步处理HTML以提取图像或链接,您将需要使用称为HTML废弃的技术

目前,最好使用


另外,关于WebClient类的文档:

这可能是您的答案:使用Server.MapPath(“//MyFiles”)+“//abc.png”作为文件名您需要使用Server.MapPath()作为文件操作所需的物理文件路径而不是虚拟路径。在您的情况下,应该使用字符串filePath=Server.MapPath(“~/MyFiles/folder/abc.png”);