Cannot access a closed Stream while creating a dow

2020-05-31 07:00发布

Im trying to prompt a downloadable text file (.txt), but I get this error:

Cannot access a closed Stream.

I have looked at simular questions in here: Cannot Access Closed Stream But it was not very useful.

Here is my code:

    private FileStreamResult Export()
    {
        string name = "filename.txt";

        MemoryStream stream = new MemoryStream();
        using (StreamWriter writer = new StreamWriter(stream))
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("A text...");
            writer.WriteLine(sb.ToString());
        }

        return File(stream, "text/plain", name);
    }

UPDATE (working copy):

This gives me an blank text file.

private FileResult Export()
{
    string name = "filename.txt";

    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);

    StringBuilder sb = new StringBuilder();
    sb.Append("A text...");
    writer.WriteLine(sb.ToString());

    writer.Flush();
    stream.Seek(0, SeekOrigin.Begin);

    return File(stream, "text/plain", name);
}

4条回答
劳资没心,怎么记你
2楼-- · 2020-05-31 07:35

Replace the FileStreamResult type on your action with FileResult.

查看更多
Anthone
3楼-- · 2020-05-31 07:40

Just remove that using statement - you are passing disposed object reference to File method and you that's the reason why you get exception. From MSDN,

The StreamWriter object calls Dispose on the provided Stream object when StreamWriter.Dispose is called.

I believe File will dispose stream by itself after usage (not verified by looking at source code).

UPDATE:

writer.Flush(); before return statement should help you

查看更多
可以哭但决不认输i
4楼-- · 2020-05-31 07:45

You have to set the position of the memorystream to 0 before using it in your FileStreamResult, otherwise it will be read from current position (IE the end of the stream).

stream.Position = 0;
return File(stream, "text/plain", name);

Just had the same thing.

I know this thread is ancient, just hoping to aid others having the same issue.

查看更多
Explosion°爆炸
5楼-- · 2020-05-31 07:54

That is correct, when you wrap a stream in another stream, calling .Close() or .Dispose() on any of them will dispose the whole stream. In this case, wrapping the MemoryStream in a StreamWriter means that when the using statement completes the StreamWriter and MemoryStream are both disposed.

My guess is since you are returning a FileStreamResult the encapsulating File will close the stream for you after the stream is no longer used. In this case, you do not want to use the using statement and will want to leave the stream open when returning it.

UPDATE

Since a stream is forward access you'll need to see the stream back to the beginning to allow the data to be read back out.

stream.Seek(0, SeekOrigin.Begin);
查看更多
登录 后发表回答