Solved

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

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

Your help would be greatly appreciated.
0
Comment
Question by:AngryC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 92
need to convert function to c# 5 53
itextsharp with c# 3 32
Create a Dropdown list with sub group 10 37
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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