Solved

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

Posted on 2006-07-14
14
1,664 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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