0

I got image content by ajax response in an array buffer.appended that array buffer to blob builder.now i want to write these contents to a file.Is there any way to do this..? I used windows.requestFileSystem it is working fine with chrome but in mozilla not working.. here is my piece of code ,

function retrieveImage(studyUID, seriesUID, instanceUID, sopClassUID,nodeRef) {

     window.requestFileSystem = window.requestFileSystem||window.webkitRequestFileSystem;
var xhr = new XMLHttpRequest();
var url="/alfresco/createthumbnail?ticket="+ticket+"&node="+nodeRef;
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
    if(this.status == 200) {
        window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
            var fn = '';
            if(sopClassUID == '1.2.840.10008.5.1.4.1.1.104.1') {
                fn = instanceUID+'.pdf';
            } else {
                fn = instanceUID+'.jpg';
            }

                    fs.root.getFile(fn, {create:true}, function(fileEntry) {
                     fileEntry.createWriter(function(writer) {
                     writer.onwriteend = function(e) {
                        console.log(fileEntry.fullPath + " created");

                    }
                    writer.onerror = function(e) {
                        console.log(e.toString());
                    }

                    var bb;
                    if(window.BlobBuilder) {
                        bb = new BlobBuilder();
                    } else if(window.WebKitBlobBuilder) {
                        bb = new WebKitBlobBuilder();
                    }
                    bb.append(xhr.response);

                    if(sopClassUID == '1.2.840.10008.5.1.4.1.1.104.1') {
                        writer.write(bb.getBlob('application/pdf'));
                    } else {
                        writer.write(bb.getBlob('image/jpeg'));
                    }
                }, fileErrorHandler);
            }, fileErrorHandler);
        }, fileErrorHandler);
    }
};
xhr.send();

}

  • you can't save files on client. you need to download it from the server. – alfred Nov 09 '12 at 09:12
  • Every time to download the file from server makes server performance very less.Any how i need to save this image content to a file please help me to solve this problem.I searched but i did n't found any filesystem for mozilla – Madhavi Mangapati Nov 09 '12 at 09:26

2 Answers2

1

The script of a web page is not allowed to write arbitrary files [such as pdfs] to client's storage. And you should be thankful because that means that web pages have a hard time trying to put malware on your machine.

Instead you should redirect the user (or open a new window/tab) to an url where the browser can find the content desired for download, and let it handle it. Use the header to tell the client to download it or displayed as explained here.

If you need to create the downloaded content dynamically, then manage it on the server making it an active page (.php, .jsp, .aspx, etc...). What matters is to have the correct MIME type in the header of the response.

Note: yes, I'm telling you to not use ajax, just window.open. Edit: I guess you may want to present the images in a img, in that case, it is the same, just put the url in the src attribute and have no ajax. Only some javascript to update the attribute if appropiate.


Given your comment I understand that you want:

  1. To cache the image in the client to avoid to have to get it back from the server every time.
  2. To allow the user to customize his experience allowing the use of images from local storage.

Now, again for security reasons, arbirary access to client's files is not allowed. In this case it works both ways: first it prevents the webpage to spy you, and second it prevents you to inject malicious content on the page.

So, for the first part, as far as I know the default is to cache images [this is handled by your browser, and yes, you should clean it from time to time because it tends to grow]. If that is not working for you, I guess you could try use a cache manifest.

About the second, the usual way would be use local storage [which, again is handled by your browser, but is not arbitrary access to client's files] to store/retrieve the url of the image and use it present the image.

The image can still be saved at the server, and yes, it can be cached. To get it to the server - of course - you can always upload it with <input type="file" ... /> and you may need to set enctype to your form. - You already knew that, right? - On the server, store the image on a database (or dedicated folder). Now the page that is resposible to retrieve the image should:

  • check the request method
  • check user's permissions (identify it by the session / cookie)
  • check the parameters of the request (if any)
  • set the header
  • output the file got the database (or dedicated folder)

Now, let's say you want to allow this to works as an xcopy deployable application (that just happens to run in a browser). In this case you can always tell the user to store the images he want in a particular location and access them with a relative path.


Or - just because - you are hosting in a place were there is no chance of server-side scripting. So you got to go along only with what javascript gives you. Well, you cannot use relative path here, since it is not local... and if you try to use a local absolute path, the browser will just diss you (I mean, it just ignores it).

So, you can't get the image from a file of the client, and you can't store it on the server...

Well, as you know there is a working draft for that, and I notice it is what you are trying. The problem is that it is a working draft. The initial implementation gets staggered by the security issues, to quote Jonas Sicking:

The main problem with exposing this functionality to the web is security. You wouldn’t want just any website to read or modify your images. We could put up a prompt like we do with the GeoLocation API, given that this API potentially can delete all your pictures from the last 10 years, we probably want something more. This is something we are actively working on. But it’s definitely the case here that security is the hard part here, not implementing the low-level file operations.

So, I guess the answer is "not yet"? In fact, considering Microsoft's approach of only providing the parts of the standardar that reach recommendation status, and also its approach of launching a new version of IE each new version of Windows... then you will have to wait a while to have supports in all the browsers. First wait until FileAPI reaches recommendation status. Then wait until Microsoft updates IE to support it. And if, by any chance (as it seems will happen) it will be only for IE10 (or a future IE11) and those deosn't work on a Windows before Windows 8, you will be waiting a lot of people to upgrade.

If this is your situation, I would suggest to get an API for some image hosting web site, and use that instead [That will probably not be free (or not be private), so you could just change your web hosting already].

Community
  • 1
  • 1
Theraot
  • 31,890
  • 5
  • 57
  • 86
  • I got the image content from server ,now what i want is if the client reloads the page it has to take it from local file system it should n't go to the server again. It is the need to work with local file system iam trying but did n't got the correct result pls help me. – Madhavi Mangapati Nov 09 '12 at 09:39
  • yeah this exactly i want.now iam trying with local storage have to see whether it works or not .Thanks for your reply and solution – Madhavi Mangapati Nov 09 '12 at 10:19
  • k boss will try for that.Thanks for your valuable suggestions – Madhavi Mangapati Nov 09 '12 at 10:37
0

you cant have a common way to store the response in files compatible with all the browsers , there is a way , u can use FileReader in javascript but that again wudn't work on IE either .

I had the similar prob a few weeks ago , what i did was i made an ajax request to a server passing the content , the server stored the content for me in the file , then it return a reference to the stored file.

i stored my files in a temp database table and the server action returned the id for the file by which we can access the file from database whenever we want.

you can also store your files on the server in some thumbnail , but i prefered database.

if u need any more specification , let me know

  • hii yeah that is good solution. but i don't want to use any server side scripting here totally i want it in javascript. – Madhavi Mangapati Nov 09 '12 at 09:43
  • within the range of my knowledge , i dont anything which can do it , the closest solution was FileReader , but IE doen't supports it , I have read in IE 10 reports that it will support it ,but upto IE9 it will not support – Hussain Akhtar Wahid 'Ghouri' Nov 09 '12 at 10:09
  • can u please give me the piece of code using file reader i already used but did n't got the result. – Madhavi Mangapati Nov 09 '12 at 10:21
  • i dont have it ryt now , but i also picked the exact same code that u might be having i also picked it from STACKOVERFLOW , since it didn't work so i didn't keep it u can post me the link and i can tell whether i used this or not – Hussain Akhtar Wahid 'Ghouri' Nov 09 '12 at 10:26
  • this is the link i used https://hacks.mozilla.org/2012/02/saving-images-and-files-in-localstorage/ – Madhavi Mangapati Nov 09 '12 at 10:34
  • yes i used this file reader part to store the file , btw when u r hitting a url via ajax to craete the buffer for the image , why dont u store it as a file simultaneously and only pass its path reference as response , just a thought , btw the link u gave me , the code was working – Hussain Akhtar Wahid 'Ghouri' Nov 09 '12 at 10:43
  • If my deductive powers don't fail me, and given that @MadhaviMangapati said "i don't want to use any server side scripting" and that the url starts with "/alfresco" I take he is using a hosting that includes the [Java based] Alfresco content mangement system (of which I know little about) and that hosting doesn't allow server side scripting. Very bad situation for a web application, IMHO. Still I don't see why opening that url in another window (and have it cached on client) won't work. – Theraot Nov 09 '12 at 11:00
  • @Theraot : If my deductive powers don't fail me, madhavi is a female name , its a SHE not HE .... lol – Hussain Akhtar Wahid 'Ghouri' Nov 09 '12 at 11:27
  • @MadhaviMangapati : is it so r u using alfresco ???? – Hussain Akhtar Wahid 'Ghouri' Nov 09 '12 at 11:27
  • yes iam using alfresco server @Hussain Akhtar Wahid . – Madhavi Mangapati Nov 09 '12 at 11:36
  • @Theraot alfresco will allow server side scripting we already used for one module. – Madhavi Mangapati Nov 09 '12 at 11:39