Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Requirement is to save a web url as .mht file and the name of the .mht file should be the title of the web page.
and all this should to be done automatically without opening of save dilog box.
and the .mht file need to save in local disk.
Posted
Updated 10-Apr-11 20:34pm
v2
Comments
Amund Gjersøe 8-Apr-11 6:38am    
Use Opera or equal to save web pages as mht. But, I'm guessing you want to do this in your own application. Have you searched using Google?
Sandeep Mewara 8-Apr-11 6:40am    
Ok. Now? What next?

Hi,
Try Below

Link[^]
 
Share this answer
 
I tried to figure this out when i first started programming in c# and quickly gave up after many searches.
Unfortuantly I not tried since to figure out a solution to this. I have worked in webrequest a bit and Base64string Conversion a great deal. I dont Know if this will help but here is a way to convert the entire page to base64string.

C#
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private byte[] downloadedData;
        public Form1()
        {            
            InitializeComponent();            
        }
        private void DownloadPage(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            
            byte[] buffer = new byte[1024];
            
            int dataLength = (int)response.ContentLength;
            
            progressBar1.Maximum = dataLength;
            lbProgress.Text = "0/" + dataLength.ToString();
            
            Application.DoEvents();
            
            MemoryStream memStream = new MemoryStream();
            while (true)
            {
                
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                if (bytesRead == 0)
                {
                   
                    progressBar1.Value = progressBar1.Maximum;
                    lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();

                    Application.DoEvents();
                    break;
                }
                else
                {
                    
                    memStream.Write(buffer, 0, bytesRead);
                                        
                    if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
                    {
                        progressBar1.Value += bytesRead;
                        lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString();

                        progressBar1.Refresh();
                        Application.DoEvents();
                    }
                }
            }
            
            downloadedData = memStream.ToArray();
            
            stream.Close();
            memStream.Close();
            
        }

        private void SavePage(string path, byte[] Data)
        {
            StreamWriter sw = new StreamWriter(path);
            sw.Write(Convert.ToBase64String(Data));
            sw.Flush();
            sw.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DownloadPage(textBox1.Text);
            //DownloadPage("http://www.codeproject.com");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;
            SavePage(sfd.FileName, downloadedData);
        }
    }
}
 
Share this answer
 
Comments
satyabrata35 11-Apr-11 2:29am    
Thanks for your effort but requirment is some different.
only we need to give the url as input and after then every thing should be done by automatically.(Means it should save the desired .mht file in local disk with taking the file name automatically as that of the title of the page)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900