C# Asp.net write file to client

前端 未结 4 1811
梦如初夏
梦如初夏 2020-11-29 06:20

I hope this is a quick question I hope. I need to write some reports and then have the user prompted to save it to his/her local machine. The last time I did this I wrote

4条回答
  •  北海茫月
    2020-11-29 07:02

    The exact answer to the question depends on how do you have your report organized (i.e. what is the "object" you're referring to). If you have a single string, you can use Response.Write to just write it. If you need a TextWriter, Response.Output is what you want - that way you can skip writing to the disk and then using WriteFile. Also, if your content happened to be binary, you could use Response.OutputStream for that.

    You may also want to set Response.AddHeader("Content-Disposition", "attachment"), if you want the user to see a save file dialog. However, Content-Disposition is not necessarily honored, so the user may still get the report opened directly in the browser. Remember to set headers before you output the actual content!

    Also, depending on the format of the report, you may want to set Response.ContentType to something appropriate such as text/plain, text/csv or whatever have you. The correct mime types are listed at the IANA site.

提交回复
热议问题