From: Garrett Smith on
Bwig Zomberi wrote:
> Ry Nohryb wrote:
>> On Apr 28, 1:49 pm, Bwig Zomberi<zomberiMAPSONNOS...(a)gmail.com>
>> wrote:
>>>
>>> Browser will have to show a dialog box. Otherwise it is a bug. Maybe you
>>> have set Safari not to show any prompts. - "Do not show this prompt
>>> again. Remember this setting."
>>
>> I can't seem to find that preference.. :-( Have you got a Safari to
>> check it out ? And yes, the dialog pops up in every other browser.
>>
>
>
> Safari does not seem to allow editing of mime-type handling. There is
> the option of saving to a folder in Preferences -> General. That's it.

WFM in Safari 4 on Windows. Did not try other versions.

An addendum can be created to explain the technique in further detail,
mention where it works and where it fails, including browser version,
user configuration, etc.

The FAQ entry should be brief and to the point. A new demo page can be
created addressing the idea of not saving the source document itself.
The faq entry and the addendum can link to that demo.

I'll add it to the list of things to do, however if somebody beats me to
it, it would be appreciated.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Garrett Smith on
Garrett Smith wrote:
> Bwig Zomberi wrote:
>> Ry Nohryb wrote:
>>> On Apr 28, 1:49 pm, Bwig Zomberi<zomberiMAPSONNOS...(a)gmail.com>
>>> wrote:
>>>>
>>>> Browser will have to show a dialog box. Otherwise it is a bug. Maybe
>>>> you
>>>> have set Safari not to show any prompts. - "Do not show this prompt
>>>> again. Remember this setting."
>>>
>>> I can't seem to find that preference.. :-( Have you got a Safari to
>>> check it out ? And yes, the dialog pops up in every other browser.
>>>
>>
>>
>> Safari does not seem to allow editing of mime-type handling. There is
>> the option of saving to a folder in Preferences -> General. That's it.
>
> WFM in Safari 4 on Windows. Did not try other versions.
>
> An addendum can be created to explain the technique in further detail,
> mention where it works and where it fails, including browser version,
> user configuration, etc.
>
> The FAQ entry should be brief and to the point. A new demo page can be
> created addressing the idea of not saving the source document itself.
> The faq entry and the addendum can link to that demo.
>
> I'll add it to the list of things to do, however if somebody beats me to
> it, it would be appreciated.
As a note to that, saving an image is a more common use case than saving
the file itself.

It is convenient to use this approach for images whose src points to an
actual image binary format, such as png, jpeg, etc. This is especially
common case for high volume sites that use a CDN.

In that case the data is not on the page as a data URI, so we want
something like:

function getDataUrl(img) {
// ...
}

The data URI can be generated by using a canvas element, and calling the
canvas element's `getDataURL` method will return a data URL for the image.

function getImageData(img) {
var cv = document.createElement('canvas'),
ctx = cv.getContext("2d");
ctx.drawImage(img,0,0);
return cv.toDataURL();
}

Unfortunately, this only works if the image was loaded on the same
domain and for a CDN, that is not going to work[1].

Is there another way to get binary data from an image?
[1]<http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#security-with-canvas-elements>
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Ry Nohryb on
On May 3, 8:07 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
> As a note to that, saving an image is a more common use case than saving
> the file itself.
>
> It is convenient to use this approach for images whose src points to an
> actual image binary format, such as png, jpeg, etc. (...)

Yeah, Smith, but it's not going to work. You can't get straight binary
data because the encoding of responseText defaults to UTF-8, and in
UTF-8 any char > 0x7f and <= 0xff is converted to utf 0xfffd -->
broken image. Maybe, just may be, you could fix that by specifying a
more appropriate charset in the content-type header of the response
(something that in general is *never* specified for images), but then
you'd have to split any chars >0x00ff into two separate chars in order
to be able to feed it to the btoa().
--
Jorge.
From: Ry Nohryb on
On May 3, 2:02 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
> On May 3, 8:07 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
>
>
> > As a note to that, saving an image is a more common use case than saving
> > the file itself.
>
> > It is convenient to use this approach for images whose src points to an
> > actual image binary format, such as png, jpeg, etc. (...)
>
> Yeah, Smith, but it's not going to work. You can't get straight binary
> data because the encoding of responseText defaults to UTF-8, and in
> UTF-8 any char > 0x7f and <= 0xff is converted to utf 0xfffd -->
> broken image. Maybe, just may be, you could fix that by specifying a
> more appropriate charset in the content-type header of the response
> (something that in general is *never* specified for images), but then
> you'd have to split any chars >0x00ff into two separate chars in order
> to be able to feed it to the btoa().
> --
> Jorge.

Some tools to see it by yourself:

function fetch (url) {
var xhr= new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send(null);
document.body.innerHTML= "<p>"+ hexdump(xhr.responseText)+ "</p>";
}

function hexdump (input, r, i) {
r= "";
if (typeof input === "string") {
i= 0;
while (i < input.length) {
r+= hexdump(input.charCodeAt(i++));
}
} else if (typeof input === "number") {
i= input.toString(16);
i= (i.length < 2) ? "0"+i : i;
r= i+ ".";
}
return r;
}

Instructions:

1.- paste and compile it in the console.
2.- fetch("urlOfAnImage")
3.- see how many .fffd(s) you get: each one is a broken byte.
--
Jorge.