我必须在我的aplication ASP.net MVC应用程序中创建并返回文件.文件类型应该是普通的.txt文件.我知道我可以返回FileResult,但我不知道如何使用它.
public FilePathResult GetFile()
{
string name = "me.txt";
FileInfo info = new FileInfo(name);
if (!info.Exists)
{
using (StreamWriter writer = info.CreateText())
{
writer.WriteLine("Hello, I am a new text file");
}
}
return File(name, "text/plain");
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用.为什么?如何使用流结果?
Big*_*ing 35
编辑(如果你想要流试试这个:)
public FileStreamResult GetFile()
{
string name = "me.txt";
FileInfo info = new FileInfo(name);
if (!info.Exists)
{
using (StreamWriter writer = info.CreateText())
{
writer.WriteLine("Hello, I am a new text file");
}
}
return File(info.OpenRead(), "text/plain");
}
Run Code Online (Sandbox Code Playgroud)
你可以试试这样的东西..
public FilePathResult GetFile()
{
string name = "me.txt";
FileInfo info = new FileInfo(name);
if (!info.Exists)
{
using (StreamWriter writer = info.CreateText())
{
writer.WriteLine("Hello, I am a new text file");
}
}
return File(name, "text/plain");
}
Run Code Online (Sandbox Code Playgroud)
将文件打开为a StreamReader
,并将该流作为参数传递给FileResult:
public ActionResult GetFile()
{
var stream = new StreamReader("thefilepath.txt");
return File(stream.ReadToEnd(), "text/plain");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
86531 次 |
最近记录: |