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

Re: Generate and save csv-file from C#? by fd123456

fd123456
Sat Jun 14 18:03:44 CDT 2008

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


On 3 juin, 16:27, "maria.elmv...@gmail.com" <maria.elmv...@gmail.com>
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)
> {
> =A0 =A0 =A0 =A0 System.Text.Encoding encoding =3D
> System.Text.Encoding.GetEncoding("ISO-8859-15");
> =A0 =A0 =A0 =A0 byte[] b =3D encoding.GetBytes(CSVContent);
>
> =A0 =A0 =A0 =A0 Response.Clear();
> =A0 =A0 =A0 =A0 Response.Buffer =3D true;
> =A0 =A0 =A0 =A0 Response.AddHeader("Content-Type", "application/ms-excel")=
;
> =A0 =A0 =A0 =A0 Response.ContentType =3D "text/csv";
>
> =A0 =A0 =A0 =A0 Response.AddHeader("Content-Disposition", "attachment;file=
name=3D" +
> DateTime.Now.ToString("dd-MM-yyyy") + "-" + QueryName + ".txt");
>
> =A0 =A0 =A0 =A0 Response.BinaryWrite(b);
> =A0 =A0 =A0 =A0 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


Re: Generate and save csv-file from C#? by MariaElmvang

MariaElmvang
Tue Jun 17 04:47:00 CDT 2008

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();

"fd123456@hotmail.com" wrote:

> 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
>
>
> On 3 juin, 16:27, "maria.elmv...@gmail.com" <maria.elmv...@gmail.com>
> 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");
> >
> > 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
>
>

Re: Generate and save csv-file from C#? by fd123456

fd123456
Sun Jun 22 07:31:24 CDT 2008

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

Michel

On 17 juin, 11:47, MariaElmvang
<MariaElmv...@discussions.microsoft.com> wrote:
> 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 bre=
ach,
> but what can I say... it works.
>
> Thanks anyway,
> Maria
>
> System.Text.Encoding encoding =3D
> System.Text.Encoding.GetEncoding("ISO-8859-15");
> byte[] b =3D encoding.GetBytes(CSVContent);
> string savePath =3D "InteressentMasse.csv";
>
> string path =3D ConfigurationManager.GetConfigKey("SavePath");
>
> System.IO.FileStream fs =3D new System.IO.FileStream(path+savePath,
> System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write,
> System.IO.FileShare.ReadWrite);
>
> System.IO.StreamWriter sw =3D new System.IO.StreamWriter(fs, encoding);
> sw.Write(encoding.GetString(b));
> sw.Flush();
>
> // close stream writer and file
> sw.Close();fs.Close();
>
>
>
> "fd123...@hotmail.com" wrote:
> > 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
>
> > On 3 juin, 16:27, "maria.elmv...@gmail.com" <maria.elmv...@gmail.com>
> > 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)
> > > {
> > > =A0 =A0 =A0 =A0 System.Text.Encoding encoding =3D
> > > System.Text.Encoding.GetEncoding("ISO-8859-15");
> > > =A0 =A0 =A0 =A0 byte[] b =3D encoding.GetBytes(CSVContent);
>
> > > =A0 =A0 =A0 =A0 Response.Clear();
> > > =A0 =A0 =A0 =A0 Response.Buffer =3D true;
> > > =A0 =A0 =A0 =A0 Response.AddHeader("Content-Type", "application/ms-ex=
cel");
> > > =A0 =A0 =A0 =A0 Response.ContentType =3D "text/csv";
>
> > > =A0 =A0 =A0 =A0 Response.AddHeader("Content-Disposition", "attachment=
;filename=3D" +
> > > DateTime.Now.ToString("dd-MM-yyyy") + "-" + QueryName + ".txt");
>
> > > =A0 =A0 =A0 =A0 Response.BinaryWrite(b);
> > > =A0 =A0 =A0 =A0 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=E9c=E9dents -
>
> - Afficher le texte des messages pr=E9c=E9dents -


Re: Generate and save csv-file from C#? by MariaElmvang

MariaElmvang
Sun Jun 22 08:10:00 CDT 2008

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


"fd123456@hotmail.com" wrote:

> Oh, you meant on the server !!! Your first code was trying to write a
> file on the *client*... Glad you solved your problem.
>
> Michel
>
> On 17 juin, 11:47, MariaElmvang
> <MariaElmv...@discussions.microsoft.com> wrote:
> > 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();
> >
> >
> >
> > "fd123...@hotmail.com" wrote:
> > > 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
> >
> > > On 3 juin, 16:27, "maria.elmv...@gmail.com" <maria.elmv...@gmail.com>
> > > 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");
> >
> > > > 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 -
>
>

Re: Generate and save csv-file from C#? by Marc

Marc
Thu Jul 10 15:40:51 CDT 2008

"MariaElmvang" <MariaElmvang@discussions.microsoft.com> wrote in message
news:EE93D321-D603-41B0-B1D8-E4A13A75D0EB@microsoft.com...
> 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