本文介绍了如何创建文件并通过FileResult在ASP.NET MVC退货吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要创建在我aplication ASP.net MVC aplication返回文件。文件类型应该是正常的txt文件。我知道我可以返回FileResult但我不知道如何使用它。
I have to create and return file in my aplication ASP.net MVC aplication. The file type should be normal .txt file. I know that i can return FileResult but i don't know how to use it.
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");
}
这code不起作用。为什么?如何使用流的结果呢?
This code doesn't work. Why? How to do it with stream result?
推荐答案
修改(如果你想流试试这个:)
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");
}
您可以尝试这样的事情。
You could try something like this..
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");
}
这篇关于如何创建文件并通过FileResult在ASP.NET MVC退货吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!