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 -
>
>