/    Sign up×
Community /Pin to ProfileBookmark

JavaScript file download of raw client-side image data

Hi all,

I have a JavaScript function which obtains some raw image data from a SWF object. I can’t modify the Flash object itself so I need to find some other way of invoking a file download dialog.

I have managed to output the data to the browser using document.write() and dumping it to an <img> element, but what I would really like is for the user to be able to click a link on the page that then calls the JavaScript function which obtains the raw data (as is already done in the existing code). I’d then like to be able to output the data in such a way as to invoke a Save File dialog (and if at all possible stay on the original page).

I’ve tried using code like

[code]document.open( ‘image/jpg’ );
document.write( image_data );
document.close();[/code]

but that shows the image data in text form. (Though I checked the headers and there is probably something wrong with this because the Content-type is being set to text/plain.)

Is such a thing possible? If not, are there any viable alternatives? (It seems a waste to post the data to the server and then dump it back out if the data is already present on the client-side.)

Any help appreciated. Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meMar 18.2009 — you can set the .src of an image to a data: url

google up "data url" and you should find what you need.

it wont work in IE6 or 7, but it will in IE8 and real browsers.
Copy linkTweet thisAlerts:
@ChipfatauthorMar 18.2009 — Thanks for the reply. That's the solution I currently have in place and working.

However, I'd like to take the 'burden' of going to another page, right clicking and choosing save as, away from the user if possible. I'd like instead to have a button/link automatically open the Save File dialog without any of the extra steps.

Because dumping the raw image data to a document object isn't working, I'm playing with the idea of posting the image data to a script on the server and dumping it there. I've only just begun looking into this but I'm guessing I can programmatically create a form object to do it?

It would still be nice not to have to go through the server to do something where the data is already on the client-side though.
Copy linkTweet thisAlerts:
@rnd_meMar 21.2009 — an unknown mime type in the data url will force the open/save dialog, but the suggested filename will be ugly...
×

Success!

Help @Chipfat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...