Merbin Joe
Merbin Joe

Reputation: 121

How to convert html strings into dataURI

I am a hybrid mobile application developer. In my application, the user can enter their own HTML, CSS and javascript code and they can get its output. Now I have the plan create an HTML file from user typed code and send it through email or other sharing application. For sharing the HTML file I am using the following cordova plugin https://www.npmjs.com/package/cordova-plugin-email#determine-if-the-device-is-capable-to-send-emails

I can successfully generate and send the HTML file through mail, using the below code

cordova.plugins.email.open({
    to:      '[email protected]',
    subject: 'Greetings',
    body:    '<h1>Test mail</h1> file generated from Test app',
    isHtml:  true,
    attachments:'base64:file.html//PGh0bWw+DQo8aGVhZD4NCjx0aXRsZT5UZXN0aW5nIHRpdGxlPC90aXRsZT4NCjwvaGVhZD4NCjxib2R5Pg0KVGhpcyBpcyB0aGUgdGVzdGluZyBib2R5DQo8L2JvZHk+DQo8L2h0bWw+'
});

But my problem is how to generate DATA URI from HTML strings? I can able to generate DATA URI from HTML file.

I used the following method to generate DATA URI Convert HTML to data:text/html link using JavaScript But it doesn't support.

Upvotes: 1

Views: 973

Answers (1)

enxaneta
enxaneta

Reputation: 33054

I don't know if you work with PHP. If you do you can use PHP like this:

$string = "I am a hybrid mobile application developer.";
$mime = "text/plain";
$base64 = base64_encode ( $string );
echo "data:$mime;base64,$base64";

If you don't work with PHP you may test the code in tehplayground and paste the output in a Firefox address bar to check the result.

I hope this helps.

Upvotes: 1

Related Questions