2013-08-24 1 views
0

는 ExpressJS 파일 업로드를 수신하는 응용 프로그램을 고려하십시오. 또한 사용하고 가능한 일부 이벤트하지만 여전히 아무런 영향을 갖고있는 것 같아요 각 파일에 대해 나에게 다음과 같은 객체를 제공 bodyParser() 미들웨어 :노드 XHR 파일 업로드 이벤트

{ 
    file: { 
     domain: null, 
     _events: {}, 
     _maxListeners: 10, 
     size: 43330194, 
     path: 'public/uploads/a4abdeae32d56a2494db48e9b0b22a5e.deb', 
     name: 'google-chrome-stable_current_amd64.deb', 
     type: 'application/x-deb', 
     hash: null, 
     lastModifiedDate: Sat Aug 24 2013 20: 59: 00 GMT + 0200(CEST), 
     _writeStream: { 
      _writableState: [Object], 
      writable: true, 
      domain: null, 
      _events: {}, 
      _maxListeners: 10, 
      path: 'public/uploads/a4abdeae32d56a2494db48e9b0b22a5e.deb', 
      fd: null, 
      flags: 'w', 
      mode: 438, 
      start: undefined, 
      pos: undefined, 
      bytesWritten: 43330194, 
      closed: true, 
      open: [Function], 
      _write: [Function], 
      destroy: [Function], 
      close: [Function], 
      destroySoon: [Function], 
      pipe: [Function], 
      write: [Function], 
      end: [Function], 
      setMaxListeners: [Function], 
      emit: [Function], 
      addListener: [Function], 
      on: [Function], 
      once: [Function], 
      removeListener: [Function], 
      removeAllListeners: [Function], 
      listeners: [Function] 
     }, 
     open: [Function], 
     toJSON: [Function], 
     write: [Function], 
     end: [Function], 
     setMaxListeners: [Function], 
     emit: [Function], 
     addListener: [Function], 
     on: [Function], 
     once: [Function], 
     removeListener: [Function], 
     removeAllListeners: [Function], 
     listeners: [Function] 
    } 
} 
내가 progress 및 complete 이벤트를 활용하는 방법을 이해하는 데 도움 싶습니다

작업.

+0

이 코드를 작성하겠다는 문서가 있습니까? 나는 그것을 다르게 보았다. (http://howtonode.org/really-simple-file-uploads). – FakeRainBrigand

+0

일반 html 형식 대신 xmlhttprequest를 사용하여 파일을 업로드하고 있습니다. 그러나 req 객체에서 데이터 이벤트를 사용할 수 있으므로 요청이 들어 오면 (?) – Dawid

+2

본문이 이미 파서 미들웨어에서 파싱됩니다. 그것을 제거하면 데이터를 가져옵니다. –

답변

2

, 요청이 이미 종료하고 업로드가 이미 완료했다. 따라서 더 이상 수신 할 데이터가 없기 때문에 data 이벤트가 더 이상 발생하지 않습니다. 또한 Jonathan Ong의 설명에 따라 읽을 수있는 스트림이 소비되었습니다. 파일 업로드 진행 상황을 찾는 다른 방법은 미들웨어, 특히 본문 파서를 사용하는 것입니다. "

defer 연기 처리 및 형태의 기다리지 않고 req.form.next()가 호출로 가공 할 양식 객체를 노출 : (Express가 연결에 내장되어 있기 때문에) 연결 문서 here 볼 때

는,이 상태 끝 "이벤트. 이 옵션은 "진행"이벤트에 바인딩해야하는 경우에 유용합니다. 다음 req.formprogress 이벤트를들을 수 있습니다, 몸 파서를 초기화 할 때

은 그래서 당신이 할 일은 참으로 defer를 설정 추가 할됩니다. 예 :

app.use(express.bodyParser({ 
    defer: true    
})); 

app.post('/upload', function (req, res) { 
    req.form.on('progress', function (received, total) { 
    var percent = (received/total * 100) | 0; 
    console.log(percent + '% has been uploaded.'); 
    }); 

    req.form.on('end', function() { 
    console.log('Upload completed.'); 
    }); 
}); 
+0

req 및 res는 여전히 스트림/이벤트 발생 자입니다. 읽을 수있는 스트림이 이미 소비되었습니다. –

+0

이를 반영하여 답변을 업데이트합니다. – hexacyanide

-1

급행 req 오브젝트와 데이터 이벤트 바인드가 없다고 생각합니다. 어디서 봤니?

또한, 당신은 업로드 할 때 사용하는 양식이 추가 시도 : 요청과 응답 객체 Express에서 액세스 할 수있는 경우에

enctype="multipart/form-data" 
+0

req 객체가 스트림입니다. 물론 데이터 이벤트가 있습니다. 시체 파서는 단지 그것을 소비합니다. –

관련 문제