Eric Valls
Eric Valls

Reputation: 81

How to store an image to my computer from SimpleHTTPServer?

I am using SimpleHTTPServer to serve a directory and run and html code locally. There, I use getusermedia api to take some pictures. If I use js localstorage to store the pictures, where are they stored exactly? How can I copy them to a known directory?

Upvotes: 0

Views: 87

Answers (1)

Patrick Sturm
Patrick Sturm

Reputation: 373

The browser usually manages the localStorage and sessionStorage variables in an encrypted, completely private area so that your browsing experience is as safe as possible (imagine if you could read and write someones files whenever they visit your web page!!).

You cannot copy the images to or from the clients computer without their knowing. You can however, cause automatic downloads server-side.

As for saving a previously downloaded image, see:

How to save an image to localStorage and display it on the next page?

However, do note, that the maximum storage space is usually capped, with sizes wildly varying between browsers and their relative environments.

My own test suggest Chromium will only support 10x 5mb files by default.

Edit:

As for copying to a known directory, you must send the file back you your http server and collect them from there. You may use ajax if you would choose, by converted the data to base64, enabling you to send the data in a json string ('failure to encode the data will results in errors'), and collect on server side with new Buffer(str,"base64").toString("binary")

var http = require('http'),
cluster = require('cluster'),
path = require('path')
url = require('url')
util = require('util')

function posthandler(request,response){
    if(request.url=="/image_streamer"){
         var datum = [];
         request.on('data',function(d){datum.push(d);});
         request.on('end',function(){
             datum = JSON.parse(datum.join("").toString('utf8'));
             datum.image = new Buffer(datum.image,"base64");
             // datum.image NOW POINTS TO A BINARY BUFFER :) HAPPY DAYS.
         });
    }
}

var server = http.createServer(function(request,response){
    switch(request.method){
         case: "GET":{} break;
         case: "POST":{posthandler(request,response);} break;
    };
}).listen(8080);

Upvotes: 1

Related Questions