0

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.

Community
  • 1
  • 1
chro
  • 1,937
  • 1
  • 17
  • 24
  • So let me see if I understand, you want to save your own site? This is to distribute to users? – Camilo Martin Nov 16 '10 at 23:16
  • The hard part is not sending the resources.. it's rendering the page on the server to look like what the client sees. – Hamish Nov 16 '10 at 23:17
  • Camilo Martin ,I simplified the task. Finally I want to convert it to pdf. The site is not static and based on closed platform. – chro Nov 16 '10 at 23:20
  • Hamish, I already done it , in my case using webkit library. – chro Nov 16 '10 at 23:22

2 Answers2

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

Shadow Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201
  • I agree with 1 & 2. But how to send send images content to server? – chro Nov 17 '10 at 13:43
  • @chro: #3 depends on the server side language which you didn't mention so far.. for example ASP.NET has WebClient class allowing you to save remote image (by URL) to the server disk. – Shadow Wizard Says No More War Nov 17 '10 at 15:04
  • ok, server side is J2EE and Apache Commons HTTPClient can do the job.How can you access remote image if they are under protected context? You may write in any server side language – chro Nov 18 '10 at 00:00
  • @chro - with the .NET WebRequest it's possible to define credentials, user name and password. Unless by "protected" you mean something else? – Shadow Wizard Says No More War Nov 18 '10 at 19:45
  • ok, but how can you access the user password from server side? If it was so easy,I can only pass url to server side, and server side will do the job without receiving innerhtml . – chro Nov 19 '10 at 14:11
  • @chro - my idea is based on the assumption you already have the password, sorry if I wasn't clear. – Shadow Wizard Says No More War Nov 19 '10 at 21:03
0

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.

Pointy
  • 389,373
  • 58
  • 564
  • 602