2

I'm writing a google chrome extension that uses

chrome.pageCapture.saveAsMHTML(object details, function callback)
function callback (blob mhtmlData) {...};

http://code.google.com/chrome/extensions/dev/pageCapture.html

which basically stores a blob representation of an mhtml page into a variable.

Now I want to let the user download this blob variable as an mhtml file..

I tried this but is gives me a 200kb file filled with random characters.

chrome.pageCapture.saveAsMHTML({tabId: sender.tab.id}, function callback(mhtml){

    var reader = new FileReader();
    reader.readAsDataURL(mhtml);

    reader.onload = function(e) {
        window.open(e.target.result);
    }


}); 
1
  • It looks like Chrome cannot show a mhtml as dataUrl. Commented Feb 12, 2012 at 7:48

2 Answers 2

4

Following is some code that I put in a page actions popup. I left the stuff that I didnt use but commented it out for reference.
EDIT:
Using the library from https://github.com/eligrey/FileSaver.js it was easy, maybe you could look at that to see what their doing.

popup.html

<html>
<head>
<script xmlns="http://www.w3.org/1999/xhtml" type="application/ecmascript" async="" src="https://raw.github.com/eligrey/FileSaver.js/master/FileSaver.min.js"></script>
<script>
function onLoad(){
var downloadLink = document.querySelector("#MHTML");

var oFReader = new FileReader();
oFReader.onload = function (oFREvent) {
// None of the following worked
  //window.open('data:application/octet-stream;'+oFREvent.target.result.slice(5));
  //window.open('data:application/message/rfc822;'+oFREvent.target.result.slice(5));
  //window.open(oFREvent.target.result);
};

chrome.tabs.getSelected(null, function(tab) {

chrome.pageCapture.saveAsMHTML({tabId: tab.id}, function (mhtml){

/// Works but requires user input
//downloadLink.setAttribute('download',tab.title+'.mhtml');
//downloadLink.setAttribute('href',window.webkitURL.createObjectURL(mhtml));

///Works but awful filename without extension
//window.open(window.webkitURL.createObjectURL(mhtml));

///Doesnt work
//oFReader.readAsDataURL(mhtml);

///Using https://github.com/eligrey/FileSaver.js , works great
saveAs(mhtml, tab.title+'.mhtml');
})
});

}
</script>
</head>
<body onload="onLoad();" style="width: 400px">

<a id="MHTML" href="#">Download Page As MHTML</a>

</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

The window.open(..) part you commented out actually works! It downloads a file named something like acb31f52-d15c-4d80-8a80-24100dfc20b0 with no extension but if you manually give it the .mht extension it can be opened. Thx! chrome.pageCapture.saveAsMHTML({tabId: sender.tab.id}, function callback(mhtml){ var url = window.webkitURL.createObjectURL(mhtml); window.open(url); }); If there was only a way to give the downloaded file a name using javascript..
@glennv You can set a filename if you use an anchor to trigger the download.
0

In case you want to give the file a name you could use an anchor element and set the name of the download attribute programmatically:

var reader = new FileReader();
reader.readAsDataURL(mhtml);

reader.onloadend = function(e) {
  const dataUrl = e.target.result;
  const a = document.createElement('a');
  a.href = dataUrl;
  a.download = fileName;
  a.click();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.