在asp.net mvc 2中显示文件
我正在尝试从网络中的服务器位置显示文本文件,但不起作用?
public ActionResult ShowFile()
{
string filepath = Server.MapPath("\\some unc path\\TextFile1.txt");
var stream = new StreamReader(filepath);
return File(stream.ReadToEnd(), "text/plain");
}
i am trying to display a textfile from a server location in the network but does not work?
public ActionResult ShowFile()
{
string filepath = Server.MapPath("\\some unc path\\TextFile1.txt");
var stream = new StreamReader(filepath);
return File(stream.ReadToEnd(), "text/plain");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
Server.MapPath("\\some unc path\\TextFile1.txt");
该文件不在您的服务器文档目录中,因此映射将失败。您有一个绝对路径,因此请在StreamReader
中使用它或直接将其提供给File()
方法。另外你的路径也不正确。请参阅另一篇文章。
The Problem is
Server.MapPath("\\some unc path\\TextFile1.txt");
The file isn't located in your server document directory, so the mapping will fail. You have an absolute path, so use this in yourStreamReader
or give it directly to theFile()
method.Also your path is incorrect. See the other post.
File
方法采用流或文件名;您正在尝试向其传递文件内容。将其更改为
The
File
method takes a stream or filename; you're trying to pass it the file contents.Change it to