Generate and save csv-file from C#?

  • Thread starter Thread starter maria.elmvang
  • Start date Start date
M

maria.elmvang

Hi all,

I need my code to be able to generate and save an csv-file without
being prompted by a dialogue box, but I don't know if such a thing is
possible. I've found out how to generate the file without problems,
but I cannot seem to make it save the file to a predetermined
location. Is such a thing even possible?

The code I use to generate and open the file is below
private void CreateResponse(string QueryName, string CSVContent)
{
System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
byte[] b = encoding.GetBytes(CSVContent);

Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Type", "application/ms-excel");
Response.ContentType = "text/csv";

Response.AddHeader("Content-Disposition", "attachment;filename=" +
DateTime.Now.ToString("dd-MM-yyyy") + "-" + QueryName + ".txt");

Response.BinaryWrite(b);
Response.End();
}

can somebody please help me figure out how I have to modify it in
order to make it save instead of just trying to open the file?

Thank you!
Maria
 
Hi Maria,

You can't. Just think : if you could do this, what would prevent
others to save a zero bytes file named "io.sys" or "config.sys" at the
root of *your* C: drive ? That would be a huge safety breach, wouldn't
it ?

The choice between saving and opening is up to the user, not the site.

(and you'd be better off asking this type of asp stuff on
microsoft.public.dotnet.framework.aspnet, btw.)

Hope this helps.

Michel
 
Hi Michel,

You can, actually. Turns out that the following code allows you to save
files on the server that hosts the code. I agree that it's a security breach,
but what can I say... it works.

Thanks anyway,
Maria

System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
byte[] b = encoding.GetBytes(CSVContent);
string savePath = "InteressentMasse.csv";

string path = ConfigurationManager.GetConfigKey("SavePath");

System.IO.FileStream fs = new System.IO.FileStream(path+savePath,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);

System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, encoding);
sw.Write(encoding.GetString(b));
sw.Flush();

// close stream writer and file
sw.Close();fs.Close();

Hi Maria,

You can't. Just think : if you could do this, what would prevent
others to save a zero bytes file named "io.sys" or "config.sys" at the
root of *your* C: drive ? That would be a huge safety breach, wouldn't
it ?

The choice between saving and opening is up to the user, not the site.

(and you'd be better off asking this type of asp stuff on
microsoft.public.dotnet.framework.aspnet, btw.)

Hope this helps.

Michel


Hi all,

I need my code to be able to generate and save an csv-file without
being prompted by a dialogue box, but I don't know if such a thing is
possible. I've found out how to generate the file without problems,
but I cannot seem to make it save the file to a predetermined
location. Is such a thing even possible?

The code I use to generate and open the file is below
private void CreateResponse(string QueryName, string CSVContent)
{
System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
byte[] b = encoding.GetBytes(CSVContent);

Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Type", "application/ms-excel");
Response.ContentType = "text/csv";

Response.AddHeader("Content-Disposition", "attachment;filename=" +
DateTime.Now.ToString("dd-MM-yyyy") + "-" + QueryName + ".txt");

Response.BinaryWrite(b);
Response.End();

}

can somebody please help me figure out how I have to modify it in
order to make it save instead of just trying to open the file?

Thank you!
Maria
 
Oh, you meant on the server !!! Your first code was trying to write a
file on the *client*... Glad you solved your problem.

Michel

Hi Michel,

You can, actually. Turns out that the following code allows you to save
files on the server that hosts the code. I agree that it's a security breach,
but what can I say... it works.

Thanks anyway,
Maria

System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
byte[] b = encoding.GetBytes(CSVContent);
string savePath = "InteressentMasse.csv";

string path = ConfigurationManager.GetConfigKey("SavePath");

System.IO.FileStream fs = new System.IO.FileStream(path+savePath,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);

System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, encoding);
sw.Write(encoding.GetString(b));
sw.Flush();

// close stream writer and file
sw.Close();fs.Close();



Hi Maria,
You can't. Just think : if you could do this, what would prevent
others to save a zero bytes file named "io.sys" or "config.sys" at the
root of *your* C: drive ? That would be a huge safety breach, wouldn't
it ?
The choice between saving and opening is up to the user, not the site.
(and you'd be better off asking this type of asp stuff on
microsoft.public.dotnet.framework.aspnet, btw.)
Hope this helps.

Hi all,
I need my code to be able to generate and save an csv-file without
being prompted by a dialogue box, but I don't know if such a thing is
possible. I've found out how to generate the file without problems,
but I cannot seem to make it save the file to a predetermined
location. Is such a thing even possible?
The code I use to generate and open the file is below
private void CreateResponse(string QueryName, string CSVContent)
{
        System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
        byte[] b = encoding.GetBytes(CSVContent);
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("Content-Type", "application/ms-excel");
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Disposition", "attachment;filename=" +
DateTime.Now.ToString("dd-MM-yyyy") + "-" + QueryName + ".txt");
        Response.BinaryWrite(b);
        Response.End();
}
can somebody please help me figure out how I have to modify it in
order to make it save instead of just trying to open the file?
Thank you!
Maria- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
 
Hi Michel,

Oh, I'm sorry I was unclear. I just wanted to save it - whether it was on
the server or on the client was of lesser importance. Sorry about that.

Maria


Oh, you meant on the server !!! Your first code was trying to write a
file on the *client*... Glad you solved your problem.

Michel

Hi Michel,

You can, actually. Turns out that the following code allows you to save
files on the server that hosts the code. I agree that it's a security breach,
but what can I say... it works.

Thanks anyway,
Maria

System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
byte[] b = encoding.GetBytes(CSVContent);
string savePath = "InteressentMasse.csv";

string path = ConfigurationManager.GetConfigKey("SavePath");

System.IO.FileStream fs = new System.IO.FileStream(path+savePath,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);

System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, encoding);
sw.Write(encoding.GetString(b));
sw.Flush();

// close stream writer and file
sw.Close();fs.Close();



Hi Maria,
You can't. Just think : if you could do this, what would prevent
others to save a zero bytes file named "io.sys" or "config.sys" at the
root of *your* C: drive ? That would be a huge safety breach, wouldn't
it ?
The choice between saving and opening is up to the user, not the site.
(and you'd be better off asking this type of asp stuff on
microsoft.public.dotnet.framework.aspnet, btw.)
Hope this helps.

On 3 juin, 16:27, "(e-mail address removed)" <[email protected]>
wrote:
Hi all,
I need my code to be able to generate and save an csv-file without
being prompted by a dialogue box, but I don't know if such a thing is
possible. I've found out how to generate the file without problems,
but I cannot seem to make it save the file to a predetermined
location. Is such a thing even possible?
The code I use to generate and open the file is below
private void CreateResponse(string QueryName, string CSVContent)
{
System.Text.Encoding encoding =
System.Text.Encoding.GetEncoding("ISO-8859-15");
byte[] b = encoding.GetBytes(CSVContent);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Type", "application/ms-excel");
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
DateTime.Now.ToString("dd-MM-yyyy") + "-" + QueryName + ".txt");


can somebody please help me figure out how I have to modify it in
order to make it save instead of just trying to open the file?
Thank you!
Maria- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
 
MariaElmvang said:
Hi Michel,

Oh, I'm sorry I was unclear. I just wanted to save it - whether it was on
the server or on the client was of lesser importance. Sorry about that.

I think this is a MIME type issue.
He wants the web browser to offer a download option rather than just showing
the file.
e.g. in GMail you have a link to view an attach files inline (if it's text
or HTML) or download the same file, even though it's a text file.
Have I misunderstood the question? ^_^

Marc
 
Back
Top