2017-10-06 13 views
1

ng-file-upload 지시어를 사용하여 각 응용 프로그램에서 서버로 이미지를 전송합니다. 여기 내 코드는 다음과 같습니다.청크 이미지 파일 업로드 문제

Upload.upload({ 
    url: 'http://localhost:5000/upload', 
    data: { file: blobFile }, 
    resumeChunkSize: 10000, 
}).then(function (resp) { //upload function returns a promise 
    console.log('resp: ', resp); 
}); 

이미지가 청크로 전송됩니다. 그러나 지금, 나는 여기에서 치게되었다. 나는이 덩어리를 받아서 전체 이미지를 만드는 방법을 모릅니다.

handler: function (req, res) { 
    var size = 0; 

    req.on('data', function (data) { 
     size += data.length; 
     console.log('Got chunk: ' + data.length + ' total: ' + size); 
    }); 

    req.on('end', function() { 
     console.log("total size = " + size); 
     res.send("response"); 
    }); 

    req.on('error', function (e) { 
     console.log("ERROR ERROR: " + e.message); 
    }); 
    } 

때마다, 나는 덩어리 요청, req.on('end', ...) 트리거를받을 다음과 같이 내 서버 코드입니다. 나는 여기에서 그렇게 혼란스러워하는 초보자입니다. 여기

답변

0

예에 .. 청크 이미지를 업로드 할 수 그렇게 간단하지 않다 ...

솔루션은 내가 사용

var request = require('request'); 
var fs = require('fs'); 

function uploadChunkedImage(url, callback){ 

    // request options 
    var options = { 
     url: url, // the url of the image 
     method: 'GET' 
    }; 

    // array of chunks 
    var chunks = []; 

    // request 
    request(options, function (err, res, body) { 
     console.log('END') 

     // body is corrupted, you can't use it for images... :-(
     // so, we will callback the concactened buffer of it instead 

     // concact chunk buffers 
     chunks = Buffer.concat(chunks); // image buffer that you can use 

     // callback the result 
     callback(err, res, chunks); 

    }).on('response', function (response) { 

     response.on('data', function (chunk) { 
      // collect chunk buffer 
      chunks.push(chunk); 
     }); 

    }); 
} 

uploadChunkedImage('http://localhost:5000/upload', function(err, res, buffer){ 
    if(!err){ 
     // save 
     fs.writeFile('./myDirPath/image.jpg', buffer); 
    }else{ 
     console.log(err); 
    } 

}); 

과 함께 프로젝트에 requestfs를 설치하는 것을 잊지 마세요

: NPM, Buffer는 더 많은 정보를위한

npm install request --save 
npm install fs --save 

기본입니다

당신은 NG 파일 업로드와 같은 트릭을 할 수 없어 내가을 이 (테스트하지) 아래에 뭔가를 시도하는 것이 좋습니다하는 good example right here가 :

handler: function (req, res) { 

    // array of chunks 
    var chunks= []; 

    req.on('data', function (data) { 

     // collect 
     chunks.push(data); 
     console.log('Got chunk: ' + data.length); 

    }); 

    req.on('end', function() { 

     // concact 
     chunks = Buffer.concat(chunks); 
     console.log("Total size = " + chunks.length); 

     // res.send() is deprecated, use res.write instead 
     res.write(chunks,'binary'); 
     res.end(null, 'binary'); 

    }); 

    req.on('error', function (e) { 
     console.log("ERROR ERROR: " + e.message); 
    }); 
} 

희망이 도움이 될 것입니다.