Save File Dialog. (I have code, just need a little help fixing)

Hey,

Ok, so I did the save FIle dialog code and it works fine. But I can't figure out how to amke it so thta is will save the contents of the current page in the webbrowser. Here is the code:

privatevoid savePageToolStripMenuItem_Click(object sender,EventArgs e)

{

Stream myStream =null;

SaveFileDialog saveFileDialog1 =newSaveFileDialog();

saveFileDialog1.InitialDirectory ="c:\\";

saveFileDialog1.Filter ="Webpage Html (*.html)|*.html|Text File (*.txt)|*.txt";

saveFileDialog1.FilterIndex = 2;

saveFileDialog1.RestoreDirectory =true;

if (saveFileDialog1.ShowDialog() ==DialogResult.OK)

{

try

{

if ((myStream = saveFileDialog1.OpenFile()) !=null)

{

using (myStream)

{

}

}

}

catch (Exception ex)

{

MessageBox.Show("Error: Could not save file from Browser. Original error: " + ex.Message);

}

}

}

Thanks :)

[2266 byte] By [progames25] at [2007-12-23]
# 1
By saying "saving the page in webbrowser" you probably want to save images and other objects in the page. You can get HTML source for the current page but you have to get other related files (image files, css, script files, if it is framed source page for each farme...). You have to create proper directories or put all the files in the same directory and change paths in the source web page.
MustafaKok at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

So how would I do that? Becuase I have only done saving rich textboxes so, doing it for a webbrowser is new to me...

Thanks :)

progames25 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Mustafa is correct. That's very hard to do, you'd have to parse each individual element to check if it contains a reference to a web server resource. It took Internet Explorer a while to get that feature.

Anyhoo, ignoring what is impossible to do:
Dim sr As New IO.StreamReader(WebBrowser1.DocumentStream)
Dim sw As New IO.StreamWriter(SaveFileDialog1.OpenFile, False)
sw.Write(sr.ReadToEnd)
sw.Close()
sr.Close()

nobugz at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

Ok, so I guess I put that inbetween the brackets of:

using (myStream)

{

}

?

Or take out what I have and replace it with that?

Thanks :)

progames25 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Yes, using using is better. Replace with this:

using (StreamReader sr = new StreamReader(webBrowser1.DocumentStream)) {

using (StreamWriter sw = new StreamWriter(SaveFileDialog1.FileName, false)) {

sw.Write(sr.ReadToEnd());

}

}

nobugz at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

I got a runtime error for this:

StreamReader sr = new StreamReader(webBrowser1.DocumentStream)

it says:

Value cannot be null.

Parameter name: steam

Thanks :)

progames25 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
Make

sure the page has finished loading (WebBrowser.DocumentCompleted event)

and that you've actually loaded a HTML page from a web server.

Try WebBrowser.Url = " http://www.google.com".

nobugz at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8

What exactly do you mean?

Thanks :)

progames25 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9

that is all true...it is loaded and works...

how about I use this:

//using (StreamReader sr = new StreamReader(webBrowser1.DocumentStream))

//{

// using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false))

// {

// sw.Write(sr.ReadToEnd());

// }

//}

saveFileDialog1.OverwritePrompt = true;

saveFileDialog1.Title = "Save Document";

saveFileDialog1.Filter = "Html File (*.html)|*.html|All Files (*.*)|*.*";

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

// Webbrowser1 save code here

}

where it says // Webbrowser1 save code here What would I put for it to save the Webpage Document...without or with images, whatever you want to give me is fine.

Thanks :)

progames25 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10

I recall coding these dialogs way back but sort of moved on, this post came up in my search

as close to my question, not coding any more unless a patch can be had and I have to as part

of the answer to this topic, which I hope you don't mind my expansion.

On the dialog, can the default option be changed without recoding?

Some file saves revert back to the last type saved, if not you are locked in to the default.

I noticed IE 7.0 has three file types .mht, HTML only .htm/.html, and Webpage complete .htm/.html

which always comes up .mht an archive file that is 10x, at last comparison, as large as normal .htm

file saves, now called complete Webpage .htm/.html.

A coded patch I might try and compile, unless some where the dialog setting is present and

you can divulge and thanks.

TeslaandLyne at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11
It's similar with this thread.
Zhi-XinYe-MSFT at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...