5

So this seems unlikely purely from a security point of view, but I need to cover my bases. Has anyone come across a method for saving an image in a browser to the user's gallery? In this case, I'm specifically talking about from site served javascript (not phonegap).

While I know you can tap and hold on the image to save, I'd love a button for our less technically inclined users.

The phone ecosystem is diverse, so any phone would work for me. I'm just curious if it's possible.

Some attempts:

Works on desktop, links through on phone (Android)

<a href="logo.png" download="logo.png">
    <img src="logo.png" alt="">
</a>

Hangs when clicked (Android)

save image to user's disk using javascript

Community
  • 1
  • 1
Mike Robinson
  • 24,971
  • 8
  • 61
  • 83
  • 2
    Here's a nice table which highlights which browsers support the download attribute. [LINK](http://caniuse.com/download) – Jnatalzia Jul 19 '13 at 14:54

1 Answers1

0

How about:

function save(){
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("pic.jpg", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {

    var photo = document.getElementById("image");
    writer.write(photo.value);

}

function fail(error) {
    console.log(error.code);
}
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147