我有一个返回文件,提供下载文件,但也将文件下载到另一个位置,我只是想提供下载到用户一个文件,即它从内存中读取初始数据,所以返回File中的第一个参数是某种类型的MemoryStream,但我无法弄清楚如何做到这一点
[HttpPost]
public FilePathResult FileToFasta(F2FModel model)
{
string FullText = new StreamReader(model.File.InputStream).ReadToEnd();
TextLayer layer = new TextLayer(FullText);
string outputFile = layer.WriteToFasta();
String mydatetime = DateTime.Now.ToString("MMddyyyy");
string FileName = String.Format("TextFile{0}.txt", mydatetime);
string FilePath = @"F:\test\" + FileName;
FileInfo info = new FileInfo(FilePath);
if (!info.Exists)
{
using (StreamWriter writer = info.CreateText())
{
writer.Write(outputFile);
}
}
return File(FilePath, "text/plain", FileName);
}
由于
答案 0 :(得分:2)
MemoryStream
可与FileStreamResult
一起使用,例如:
[HttpPost]
public FilePathResult FileToFasta(F2FModel model)
{
string FullText = new StreamReader(model.File.InputStream).ReadToEnd();
TextLayer layer = new TextLayer(FullText);
string outputFile = layer.WriteToFasta();
string mydatetime = DateTime.Now.ToString("MMddyyyy");
string FileName = String.Format("TextFile{0}.txt", mydatetime);
//Use different encoding if needed
byte[] outputArray = Encoding.Unicode.GetBytes(outputFile);
MemoryStream outputStream = new MemoryStream(outputArray);
//FileStreamResult will close the stream for you so don't worry
return new FileStreamResult(outputStream, "text/plain") { FileDownloadName = FileName };
}