Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a simple webform, which the user can select values via dropdowns and textboxes. Upon button click event, all data needs to save as csv to a network shared folder, with a random unique filename. I can create the csv file just fine, but am looking for a little direction on how to save to network share without any prompts to the user. Code-behind is below:

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace AegisSOR
{
public partial class SOR : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename={0}.csv", DateTime.Now));
Response.Write(Label1.Text);
Response.Write(",");
Response.Write(Label2.Text);
Response.Write(",");
Response.Write(DropDownList1.Text);
Context.Response.End();
}
}

}
Posted
Updated 5-May-16 3:45am

1 solution

You're putting the csv into the Response. The Response is what is sent back to the user.

All you have to do is use the File object, File Class (System.IO)[^]. It's very easy. Build up your csv in a StringBuilder and then use File.WriteAllText().

The biggest problem here is the account running the app pool in IIS is the account your code will execute as. That account will need write permissions to the network share.
 
Share this answer
 
Comments
martyres72 5-May-16 9:46am    
thanks for the quick response. I haven't used a stringbuilder......can you add a small example of code, while I research.
ZurdoDev 5-May-16 10:26am    
It's better to use a StringBuilder rather than concatenate over and over into a single string.

This site has great examples for everything c#, http://www.dotnetperls.com/stringbuilder
martyres72 5-May-16 10:30am    
Excellent info. Thanks for putting me on track.
ZurdoDev 5-May-16 10:46am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900