Solved

How can I display a .mht file on a webBrwoser control?

Posted on 2006-07-14
14
1,586 Views
Last Modified: 2012-08-30
The title says it!

Your help would be greatly appreciated.
0
Comment
Question by:AngryC
  • 5
  • 4
  • 4
14 Comments
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17113402
Ok, I've got to ask: did you mean .htm?

I'm wondering if the issue is the file or how to display it.

Thanks.

Jason
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17114242
I believe that the title is correct--.mht and not .htm.  mht is a single-file type that has a structure that doesn't require external files, like images.  What happens if you navigate to the .mht file?

Bob
0
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17114571
Thanks Bob.

AngryC,

You should be able to set the .Url property of the WebBrowser control to the uri (translate: path) of the file. So, if for example, the .MHT file (test.mht) was on your desktop, the code would be:

webBrowser1.Url = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.mht");

Thanks.

Jason
0
 

Author Comment

by:AngryC
ID: 17114702
Thanks for your follow-ups.

I acually know it's possible to load a file if it's saved. But what I'm trying to do is to load the mht file from resource as follow:

webBrowser1.Document.Body.InnerHtml = global::WindowsApplication1.Resource1.MyMhtFileName;

What I am getting is a page that looks like it was opened with notepad not with IE!!!

Please help.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17114835
1) >>opened with notepad
What does that mean?  Are you losing the HTML formatting?

2) What is WindowsApplication1.Resource1.MyMhtFileName returning?

3) What encoding are you using?

Bob

0
 

Author Comment

by:AngryC
ID: 17114851
1) Yes. I'm losing the HTML formatting.

2) The file source (string).

3) I'm not specifying any but I'm sure it's not a matter of encoding. I think I should change the MIME type the webBrowser control. But I don't know how...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17114888
1) This is header from an .mht file:

From: <Saved by Microsoft Internet Explorer 5>
Subject: Online Account Services
Date: Mon, 19 Dec 2005 08:41:51 -0500
MIME-Version: 1.0
Content-Type: multipart/related;
      type="text/html";
      boundary="----=_NextPart_000_0000_01C60478.0E7B3520"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180

This is a multi-part message in MIME format.

2) How did you embed the value for MyMhtFileName?

Bob
0
 

Author Comment

by:AngryC
ID: 17114917
There's no problem with my mht file, it's working when I'm doing this:

webBrowser1.Url = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.mht");

I'm also getting the exact content file the file from the resource without any problem, the problem is how can I send it to the webBrowser control?
0
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17115669
Is saving the contents back to a file an option? If so, you could write the resource back to a file in the temp folder and then give the browser control its path. Just a thought.

Jason
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17115684
I didn't say that there was a problem with .mht.  It looks like you are trying to extract the file as an embedded resource from the assembly, so I am trying to figure out how it is getting mangled.

Bob
0
 

Author Comment

by:AngryC
ID: 17116306
I want to load it directly from the resource. I don't want to save the file on desk before using it.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17129317
Yes, but when you are loading it from the resource, then it doesn't look correct, so I am trying to figure out why.

Bob
0
 
LVL 7

Accepted Solution

by:
whatsit2002 earned 50 total points
ID: 17134304
It looks to me like you're out of luck for now. Until Microsoft lets us set the MIME type of the document, you may have to do something like this:

            //get the bytes of the MHT file from the resources of this application
            byte[] mht = global::MyNamespace.Properties.Resources.Testing;
            string filename = Environment.GetEnvironmentVariable("TEMP") + @"\tmp.mht";
            //save the bytes back to a file
            File.WriteAllBytes(filename, mht);
            //load the file in the browser
            webBrowser1.Url = new Uri(filename);

I cannot come up with a way to tell the web browser control what MIME type it is suppose to be using to open the memory stream. Bob, have you had any luck?

Thanks.
Jason
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now