file
이벤트
router.post('/contents/insert/upload', ensureAuthenticated, function(req, res, next) {
\t
\t var form = new formidable.IncomingForm();
\t
\t form.parse(req);
// \t form.on("fileBegin", function (name, file){
// \t \t console.log('upload come on3');
// \t \t
// });
form.on("file", function (name, file){
fs.readFile(file.path, function(error, data){
\t var filePath = __dirname + '/../public/uploads/' + file.name;
\t
\t fs.writeFile(filePath, data, function(error){
\t \t if(error){
\t \t \t throw err;
\t \t \t //res.redirect('back');
\t \t }else {
\t \t \t res.redirect('back');
\t \t }
\t });
});
});
});
<form action="/adm/contents/insert/upload" method="post" enctype="multipart/form-data" >
\t \t \t <!-- <input type="file" name="file" />
\t \t \t <input type="submit" /> -->
\t \t \t \t <div class="file-field input-field">
\t \t \t \t <div class="btn">
\t \t \t \t \t \t <span>input images</span>
\t \t \t \t \t <input type="file" name="file" multiple>
\t \t \t \t </div>
\t \t \t \t <div class="file-path-wrapper">
\t \t \t \t <input class="file-path validate" type="text">
\t \t \t \t </div>
\t \t \t </div>
\t \t \t <input type="submit" class="btn waves-effect waves-light" value="upload" />
\t \t \t </form>
업로드의 모든 파일에 대해 트리거됩니다, 그래서 결국 코드는 각 업로드 된 파일에 대한
res.redirect()
를 발행합니다. 이로 인해 오류가 발생합니다 (요청 수명 동안 리디렉션 만 보내거나 응답을 한 번만 보낼 수 있음).
대신, 당신은 end
이벤트를 수신하고,이 리디렉션 발행 할 :
form.on("file", function (name, file) {
...handle the file copy here, but don't call `res.redirect()` anywhere...
});
form.on("end", function() {
res.redirect('back');
});
FWIW을, 당신이 대신 가공 할의 uploadDir
옵션을 설정하거나 fs.rename()
을 사용하려는 나에게 보인다 업로드 후 파일을 읽고 새로운 위치에 쓴다.
나는 {(함수를 "끝"() { \t \t \t \t res.redirect) ('다시'; \t \t \t \t \t \t \t form.on을})} 다른 코드를 추가; \t \t} 작성한 내용. 감사합니다. 하지만 브라우저가 계속 로딩되는 이유를 모르겠다 ... 심지어 삽입했기 때문이다. –
당신은'end' 핸들러를'file' 핸들러와 _inside_하지만 같은 레벨에 추가하면 안된다. 내가 명확하게 대답을 수정합니다. – robertklep
고마워요 !! 그것은 일하고있어 명확히 !! 당신의 대답을 선택했습니다. –