开发者

Open csv file and display it to user (.Net)

In a .net website project, I have a csv-file (located in app_data folder of the project) to which I write data after user has clicked a button. Now I want to open the file and display the content to the user (like it would happen if he clicked on it in the windows shell), so that user can check data and save the file to th开发者_开发知识库e folder he wants. How can I accomplish that?


Why do you expect that the server is able to open a file on the client? Opening an arbitrary file with the associated shell handler would be a big security hole.

You can display the data as part of your website and then offer a download.


It is necessary to redirect the server response to the csv-file.

string path = context.Server.MapPath(String.Format(@"~/App_Data/" + csvFile));

using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                long FileSize = fs.Length;

                byte[] Buffer = new byte[(int)FileSize];
                fs.Read(Buffer, 0, (int)FileSize);

                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment;filename=Export.csv");
                Response.ContentType = "text/csv";
                Response.BinaryWrite(Buffer);

                Response.Flush();
            }

The client receives the file and can open and edit it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜