Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to open the "Save As" browse box for saving files

Status
Not open for further replies.

christheprogrammer

Programmer
Jul 10, 2000
258
CA
Hey all,
I have a form where the user chooses some things, and a query is run. The resulting data is in a datagrid (via dataset) and i want to have a button which launches the familiar "Save As" dialog which allows the user to browse his/her file system and save the file anywhere on the hard drive.
I know how to do the file writing, etc. I just need to know how to open up this dialog (I believe it is just a function call which returns the path the user chose)...

Anyone know?
Thanks in Advance Chris
 
This is not as easy as you may be expecting. First of all certain browsers will want to open the file - depending on the type. The best way to complete this task with certainty is to create a link on the page and require your user to right click and save as.

Trust me I worked this problem and I spent a week on it - and in the end I ended up with a popup window with a link for them to right click and save as. Any approach I took that worked in one browser did not work in another.

If you have more luck than me I hope you will share it here, as my old project was in cold fusion and maybe you will find something in .net that will make it easier. Crystal
crystalized_s@yahoo.com

--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Thanks, I thought it might be a problem. I'll re-post if I figure it out

CHEERS! Chris
 
code in c# for Windows Forms

SaveFileDialog fdlg = new SaveFileDialog();

fdlg.CheckFileExists = false;
fdlg.Title = "XML Creator - Select xml file" ;
fdlg.InitialDirectory = @"c:\" ;
if(fdlg.ShowDialog() == DialogResult.OK)
{
// do something
}
fdlg.Dispose();


hth
 
I read something the other day that said if you set the content-type to octet-stream, then that will force the browser to open the dialog even if the browser would have recognized the mime type.

I just tried to find the article but was unable...

Maybe you'll have better luck...

:)
paul
penny1.gif
penny1.gif
 
Okay, I found an article describing how to enable the File Save Dialog. The command is:

Response.AddHeader("content-disposition","attachment; filename=fname.ext")

which troubles me because it is classic ASP, not ASP.NET.
(Anyone know the proper ASP.NET command for this?)

One problem I found with this so far is the filename. I use:

Response.AddHeader("content-disposition","attachment; filename=" & MapPath("fname.ext"))

and have used the debugger to check that MapPath finds the correct path. But the actual download gets the current web page html file instead of the file I want. What's up with that?
If you know please pass the info down the grapevine

Thanks again everyone!
Chris
 
Figured it out. For everyone's info, the button on_click event handler must look like this (in VB):

Private Sub saveAsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveAsButton.Click
Dim filename As String = MapPath("filename.txt")
Dim contentString As String = "attachment; filename="
contentString &= """"
contentString &= filename
contentString &= """"
Response.ContentType = "Application/zip"
Response.AddHeader("content-disposition", contentString)
Response.WriteFile(filename)
Response.End()
end sub

The quotes are important for this - it doesn't work without them...

Have a great weekend! Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top