Sergei Chicherin
Sergei Chicherin

Reputation: 2050

"save as" html on server side

First of all this is not a question about how can I use http client on server, it's more about JS.

I have a protected area on site (let say /myprotected/ ), all css and images are on it. I want to html post it all (let say /htmlconverter) . And on server side finally convert it to looks like offline page. Something like "save as" in browser. Here is the code for html text only

function submitForm(){
var htmlelement = document.getElementById("html");
htmlelement.value = document.getElementById("source").innerHTML;
document.forms["innerForm"].submit();

}.....

<form id="innerForm" method="post" action="/htmlconverter">
   <input type="text" id="html" name="html" style="display:none"/>
   <input type="button" value="submit" onclick="submitForm()"/>
  </form>

Is there a way to send all resources including img and css to server?

Added: I find this topic on serializing images Get image data in JavaScript?

For css I will try to AJAX call the css source and add response as a text to a new post.

Upvotes: 0

Views: 1871

Answers (2)

Pointy
Pointy

Reputation: 413709

You would have to do this from your server somehow. In other words, you'd have to post back some sort of command to the source of your stuff, and tell it to post (as a multipart form or something) whatever needs to be posted to the target server.

The browser can't post things like image data and CSS bodies that happen to be associated with a page.

edit In modern browsers, it's possible to grab image data by writing images to a canvas, though it's not possible to access the image data if they're sourced from a different domain than that of the main page.

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

  1. Add ID to the <html> tag itself, e.g. <html id="MyHTML">.

  2. Send its innerHTML to the server.

  3. On the server, parse the raw HTML and extract CSS files by parsing the "href" attribute of <link rel="stylesheet" ... /> and images by parsing the "src" attribute of <img> tags. Should be possible using regular expressions.

Upvotes: 1

Related Questions