1

我有一个返回文件,它提供下载文件但也将文件下载到另一个位置,我只是希望它提供给用户一个文件下载,即它从内存中读取初始数据,所以第一个参数在返回文件中是某种 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);

    }

谢谢

4

1 回答 1

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 };
}
于 2012-11-13T16:50:05.370 回答