c# - 不支持 URL 格式

标签 c# asp.net

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

   http://localhost:10001/MyFiles/folder/abc.png

我也试过,但是没有成功

http://localhost:10001//MyFiles//abc.png

但它的给予

URL Formats are not supported

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

如何为 Http 路径提供文件路径?

这是我的代码

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

最佳答案

看看 WebClient ( MSDN docs ),它有很多从网络下载数据的实用方法。

如果您希望资源作为,请尝试:

using(WebClient webClient = new WebClient())
{
    using(Stream stream = webClient.OpenRead(uriString))
    {
        using( StreamReader sr = new StreamReader(stream) )
        {
            Console.WriteLine(sr.ReadToEnd());
        }
    }
}

关于c# - 不支持 URL 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17918433/

相关文章:

jquery - 如何从多选列表框中获取选定的项目以使其被选中?

c# - 网站登录和抓取 HTML

c# - 如何处理 DynamicObject 中的通用方法调用?

c# - MVC 中的 json 对象绑定(bind)

asp.net - 如何从ASP.NET 3.5中的Web内容页面设置页面标题

asp.net - 需要一些有关 TFS2010 + 自动构建 + 'Configurations to Build = Debug' 的帮助

c# - 从 session Cookie 实例化 ASP.NET session

c# - 无法从 web api 获取文件

c# - Entity Framework 多对象上下文

.net - ASP.NET MVC - 获取 QueryString 值