2014-05-17 3 views
0

이제는 파일 업로드에 각도 파일 업로드 패키지를 사용하고 있습니다. item.upload()를 누르면 파일이 성공적으로 업로드되었다고 주장하지만 req.body가 비어있는 것을 볼 수 있습니다. 도와주세요! 여기 AngularJS에서 ExpressJs로 파일을 업로드하지 못했습니다.

그것을 처리하기 위해 각 코드 :

var uploader = $scope.uploader = $fileUploader.create({ 
     scope: $scope,       // to automatically update the html. Default: $rootScope 
    url: '/api/teams/upload', 
    formData: [ 
     { key: 'value' } 
    ], 
    filters: [ 
     function (item) {     // first user filter 
      $scope.previewImage(item); 
      return true; 
     } 
    ] 
}); 

을 그리고 여기에 업로드 트리거 할 수있는 방법입니다

uploader.bind('afteraddingfile', function (event, item) { 
    // console.info(item.file); 
    console.info('After adding a file', item); 

    // console.log('item.upload();'); 
    item.upload(); 
}); 

을 그리고 마지막으로 여기에 명시 JS 코드 :

exports.upload = function(req, res) { 
// console.log('req.headers'); 
// console.log(req.headers); 

console.log('req.body'); 
console.log(req.body); 

무엇이 잘못 되었나요? 당신은 단지에 필요한 익스프레스의 이전 버전에서

var bodyParser  = require('dy-parser'); 
//... 

var app   = express(); 
//... 
app.use(bodyParser()); // pull information from html in POST 

var busboy = require('connect-busboy');  
app.use(busboy()); 

:

익스프레스 사에서 당신이 당신의 서버에서 body parser을 설정해야 enctype="multipart/form-data"로 ....

답변

0

첫째는 POST 인코딩되어 있는지 확인 프레임 워크 자체에서 본문 파서를 추가하면 파일이 구성된 위치에 저장됩니다.

 app.use(express.bodyParser({limit: '10mb', uploadDir: __dirname + '/public/uploads' }));    // pull information from html in POST 

버전 4에서 연결 지원이 제거되었으므로 를 시작하려면 당신이 $upload.upload를 호출 할 필요가 클라이언트 측에서

var fs = require('fs'); 
var busboy = require('connect-busboy'); 

//... 
app.use(busboy()); 
//... 
app.post('/api/teams/upload', function(req, res) { 
    var fstream; 
    req.pipe(req.busboy); 
    req.busboy.on('file', function (fieldname, file, filename) { 
     console.log("Uploading: " + filename); 
     fstream = fs.createWriteStream(__dirname + '/files/' + filename); 
     file.pipe(fstream); 
     fstream.on('close', function() { 
      res.redirect('back'); 
     }); 
    }); 
}); 

: 지금 당신은 당신이 뭔가를해야 할 것이다, 그래서 멀티/부품 게시물을 파서 다중/폼 데이터에 대한 사용자 지정 지원을 추가해야 업로드

관련 문제