Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to load an JSZip uncompressed file to DWV? #332

Closed
arturojain opened this issue Apr 25, 2017 · 5 comments
Closed

how to load an JSZip uncompressed file to DWV? #332

arturojain opened this issue Apr 25, 2017 · 5 comments
Labels
question Further information is requested
Milestone

Comments

@arturojain
Copy link

arturojain commented Apr 25, 2017

I'm trying this but it doesn't seem to work

JSZip.loadAsync(data)
	.then(function (zip) {
		zip.file('1.dcm').async("arraybuffer").then(function (file) {
			app.loadFiles([file])
		})	
	})

I think it has something to do with the file format.

@ivmartel ivmartel added the question Further information is requested label Apr 25, 2017
@ivmartel ivmartel added this to the 0.20.0 milestone Apr 25, 2017
@ivmartel
Copy link
Owner

The loadFiles method takes as input a list of (web API) Files as defined here. It will not work with an arraybuffer.

What would need to be done is more like what you can find in io/file.js, which is not that well designed... If you know of a better one, I'm interested!

@tmskmr
Copy link
Contributor

tmskmr commented Apr 27, 2017

Hi, I have done it successfully as follows:

  1. create a new class "dwv.io.Memory" from dwv.io.File (see the attached file)
    memory.zip

  2. add a new function loadImagesFromMemory() to dwv.App

    this.loadImagesFromMemory = function (files)
    {
        // clear variables
        self.reset();
        nSlicesToLoad = files.length;
        // create IO
        var memIO = new dwv.io.Memory();
        memIO.setDefaultCharacterSet(defaultCharacterSet);
        memIO.onload = function (data) {
            if ( image ) {
                view.append( data.view );
                if ( drawStage ) {
                    appendDrawLayer(image.getNumberOfFrames());
                }
            }
            postLoadInit(data);
        };
        memIO.onerror = function (error) { handleError(error); };
        memIO.onloadend = function (/*event*/) {
            if ( drawStage ) {
                activateDrawLayer();
            }
            fireEvent({ 'type': 'load-end' });
        };
        memIO.onprogress = onLoadProgress;
        // main load (asynchronous)
        fireEvent({ 'type': 'load-start' });
        memIO.load(files);
    };
  1. call the above function as follows
var filename = "";
var files = [];
var zobjs = null;

/**
 * JSZip.async callback
 * @param {ArrayBuffer} content unzipped file image
 * @return {}
 */
var prefixUnzip = "Unzipping... ";
function zipAsyncCallback(content)
{
	files.push({"filename": filename, "data": content});
	dwv.gui.displayProgress(Math.round(files.length*100/zobjs.length), prefixUnzip);

	if (files.length < zobjs.length){
		var num = files.length;
		filename = zobjs[num].name;
		zobjs[num].async("arrayBuffer").then(zipAsyncCallback);
	}
	else
		TheApp.loadImagesFromMemory(files);
}

/**
 * JSZip.loadAsync callback
 * @param {JSZip} zip JSZip object
 * @return {}
 */
function loadAsyncCallback(zip)
{
	files = [];
	zobjs = zip.file(/.*\.dcm/);

	var num = files.length;
	filename = zobjs[num].name;
	zobjs[num].async("arrayBuffer").then(zipAsyncCallback);
	dwv.gui.displayProgress(0, prefixUnzip);
}

/**
 * Load images in DMX file after download from URL
 * @param {String} url URL to download
 * @return {}
 */
var prefixDownload = "Downloading... ";

function loadFromDmxFile( url )
{
	dwv.gui.displayProgress(0, prefixDownload);

	JSZipUtils.getBinaryContent(url, function(err, data) {
		if(err) {
			alert(err.name+": "+err.message+".");
		}

		dwv.gui.displayProgress(100);
		JSZip.loadAsync(data).then(loadAsyncCallback);
	}, function( event ) {
		if (event.lengthComputable){
			dwv.gui.displayProgress(Math.round(event.loaded*100.0/event.total), prefixDownload);
		}
	});
}

This was referenced May 5, 2017
@ivmartel
Copy link
Owner

ivmartel commented May 8, 2017

I adapted your code to the new design, see #339.

One difference is the zipped file extension, you use dmx, what is it?

@tmskmr
Copy link
Contributor

tmskmr commented May 9, 2017

Thank you for your adaptation. I am glad to make some contribution to this software.
The dmx extension is used locally in a DICOM images database site for associating the zipped file to the program which unzip it and invoke a standalone viewer. You may ignore it.

@ivmartel
Copy link
Owner

ivmartel commented May 9, 2017

Thanks to you! Closing this issue for now, feel free to reopen if you see a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants