Ali_dotNet
Ali_dotNet

Reputation: 3279

save created excel file on server

I'm creating an excel file using Respose.Write in ASP.NET C#, my users can save the created file on their systems, but I want to save this excel file on my server, without user knowing anything about it. how is it possible? this is how I create my Excel file:

        Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";

....

    Response.Write("</table>");
    Response.Flush();
    Response.End();

this code saves created excel file client PC, but I want to save created file on the server

Upvotes: 0

Views: 8831

Answers (1)

dana
dana

Reputation: 18125

The general idea is to write the contents of the Excel file to a buffer. Then a) save it to a file and b) stream it to the client. You will want to use a unique file naming convention on the server (include a time stamp for example). The IIS App Pool also needs write permission on the folder where you are saving the file to.

StringBuilder excel = new StringBuilder();
excel.Append("<table>");
...
excel.Append("</table>");

// save to file
File.WriteAllText("C:\\blah.xls", excel.ToString());

// output to response
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(excel.ToString());
Response.End();

Upvotes: 2

Related Questions