Cannot access a closed Stream while creating a downloadable text file in ASP MVC 3

前端 未结 4 740
臣服心动
臣服心动 2021-02-06 23:06

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

Cannot access a closed Stream.

I have looked at simular q

4条回答
  •  遇见更好的自我
    2021-02-07 00:08

    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

提交回复
热议问题