1

I have some data in the form of a string. I want to write this data to a file and save the file to the specified path. The path would be specified by opening a save as dialog on the button click. How can this be achieved??

1
  • Presumably you are trying to allow someone to save the file client-side and not server-side? Commented Aug 4, 2009 at 11:56

5 Answers 5

2

The file is saved into the server initially with this code

string getnote = txtdisplay.Text.Trim();
        String filepath = Server.MapPath(@"img\new1.txt");
        System.IO.FileStream ab = new System.IO.FileStream(filepath, System.IO.FileMode.Create);
        System.IO.StreamWriter Write101 = new System.IO.StreamWriter(ab);
        Write101.WriteLine(getnote);
        Write101.Close();
        Response.ClearContent();

From the server get the file as attachment.Use the following code for save as dialog box for downloading or saving the file. The file will save by default in the download folder. To save to the specified location change the browser settings.

Response.ContentType = "text";
        Response.AppendHeader("Content-Disposition", "attachment; filename=new1.txt");
        Response.TransmitFile(Server.MapPath("~/img/new1.txt"));
        Response.End();
Sign up to request clarification or add additional context in comments.

Comments

1
Response.ContentType = "application/octet-stream" (or content type of your file).
Response.AppendHeader("content-disposition", "attachment;filename=" & strFileName)

2 Comments

I think you'd better show how to write the content. This sounds like a beginner.
Could you please explain this a bit more. how will this show the saveas dialog
1

There is no Save As dialog in ASP.NET.

Remember, your ASP.NET application is running in a browser on a user's computer. You have no access to the user's file system, including the Save As dialog.

However, if you send the user a file, as an attachment, most browsers will display a dialog asking the user whether to save the file or open it. Maybe the user will choose to save it. That's what the example from phoenix does.

Comments

0

You could use a LinkButton (or regular link) and have the url point to a handler (ASHX) that retrieves the data and sends back a response with content disposition set to attachment. Write the data to the response. You'll also need to set up some other headers in the response -- such as content type and length. This would give the document (file) a regular link that could perhaps be bookmarked (if a regular link) in the future so that it can be retrieved again. You'd need to pass enough data in the query string to be able to identify which data is to be downloaded.

Comments

-1

if I userstand you correctly, here -

saveFileDialog1.DefaultExt = "*.file";
        saveFileDialog1.Filter = "File|*.file|Other File|*.OFile|";
        if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
           saveFileDialog1.FileName.Length > 0)
        {
            WebClient wc = new WebClient();
            wc.DownloadFile("http://www.exaple.com/exaplefile", saveFileDialog1.FileName);;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.