2014-12-01 9 views
7

sto provando a:utilizzando zip.js di leggere un file zip tramite XMLHTTP/chiamata AJAX su Node.js

  1. Invia un file zip tramite XMLHTTP al cliente
  2. quindi leggere il file usando zip.js e rendere il suo contenuto

ricevo con successo il binario del file cioè il callback successo si chiama ma ottengo ed errore quando provo a fare GetEntries. Penso che l'errore è con il modo di inviare stream, per favore aiutatemi.

errore msg:

Errore nella lettura del file zip

Il mio codice lato client (utilizzando angolare):

$http.get(window.location.origin + '/book/'+bookName,{responseType:"Blob"}). 
success(function (data , error) { 
    var a = new Uint8Array(data); 
     //var dataView = new DataView(data); 
     //var blob = new Blob(dataView.buffer); 
    zip.useWebWorkers = true; 
    zip.workerScriptsPath = '/js/app/'; 

    zip.createReader(new zip.BlobReader(data), function(reader) { 

     // get all entries from the zip 
     reader.getEntries(function(entries) { //HERE I GET THE ERROR 
     if (entries.length) { 

      // get first entry content as text 
      entries[0].getData(new zip.TextWriter(), function(text) { 
      // text contains the entry data as a String 
      console.log(text); 

      // close the zip reader 
      reader.close(function() { 
       // onclose callback 
       var a = 0; 
      }); 

      }, function(current, total) { 
      // onprogress callback 
      var a = 0; 
      }); 
     } 
     }); 
    }, 
    function(error) { 
     // onerror callback 
     var a = 0; 
    }); 


}) 
.error(function (data , error) { 
    var a = 0; 

}); 

mio codice lato server sul nodo:

router.get('/book/:bookName',function (req , res) { 


console.log('Inside book reading block : ' + req.params.bookName); 
req.params.bookName += '.zip'; 

var filePath = path.join(__dirname,'/../\\public\\books\\' ,req.params.bookName); 
var stat = fileSystem.statSync(filePath); 

res.writeHead(200, { 
    //'Content-Type': 'application/zip', 
    'Content-Type': 'blob', 
    'Content-Length': stat.size 
}); 

var readStream = fileSystem.createReadStream(filePath); 
// replace all the event handlers with a simple call to readStream.pipe() 
readStream.pipe(res); 
}); 

risposta

2

È probabile che tu abbia già trovato una soluzione. Ho affrontato lo stesso problema di oggi e questo è come ho risolto in javascript pianura:

var xhr = new XMLHttpRequest(); 
xhr.open('GET', 'assets/object/sample.zip', true); 
xhr.responseType = 'arraybuffer'; 

xhr.onload = function(e) { 
    // response is unsigned 8 bit integer 
    var responseArray = new Uint8Array(this.response); 
    var blobData = new Blob([responseArray], { 
     type: 'application/zip' 
    }); 
    zip.createReader(new zip.BlobReader(blobData), function(zipReader) { 
     zipReader.getEntries(displayEntries); 
    }, onerror); 
}; 

xhr.send(); 

Il problema che vedo nel codice è che si sta modificando il valore di Uint8Array e assegnandolo a var a, ma ancora utilizzare il dati grezzi in blobreader. Anche il lettore blob richiedeva il blob e non un array. Quindi dovresti convertire var a in blob e quindi usarlo per la lettura.

+0

A valle senza un commento. Potresti almeno condividere la ragione. – Sandeep

Problemi correlati