HTML
페이지에서 이미지 파일을 업로드해야합니다. 그러나 나중에 form
필드 (텍스트 필드, 체크 박스 등)가 있기 때문에 나중에 서버에 데이터를 전달하는 데 사용되므로 form
태그를 사용하지 않을 것입니다.JQuery에서 Node JS로 이미지 업로드
내 백 엔드는 Node JS
입니다. 내가 원하는 것은 Node Js
끝에서 이미지 데이터 (파일)를 검색하는 것입니다. 나는
router.post('/imgup', function(req, res){
console.log(' File name '+req.files.upload);
});
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default">
<div class="panel-body">
<span class="glyphicon glyphicon-cloud-upload"></span>
<h2>File Uploader</h2>
<h4>coligo.io</h4>
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
</div>
<button class="btn btn-lg upload-btn" type="button">Upload File</button>
</div>
</div>
</div>
</div>
</div>
<input id="upload-input" type="file" name="uploads" multiple="multiple"></br>
JQuery와
$('.upload-btn').on('click', function(){
$('#upload-input').click();
$('.progress-bar').text('0%');
$('.progress-bar').width('0%');
});
$('#upload-input').on('change', function(){
var files = $(this).get(0).files;
if (files.length > 0){
// create a FormData object which will be sent as the data payload in the
// AJAX request
var formData = new FormData();
// loop through all the selected files and add them to the formData object
for (var i = 0; i < files.length; i++) {
var file = files[i];
var tmppath = URL.createObjectURL(event.target.files[0]);
// add the files to formData object for the data payload
formData.append('uploads', tmppath);
}
$.ajax({
url: '/myp/imgup',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data){
console.log('upload successful!\n' + data);
},
xhr: function() {
// create an XMLHttpRequest
var xhr = new XMLHttpRequest();
// listen to the 'progress' event
xhr.upload.addEventListener('progress', function(evt) {
if (evt.lengthComputable) {
// calculate the percentage of upload completed
var percentComplete = evt.loaded/evt.total;
percentComplete = parseInt(percentComplete * 100);
// update the Bootstrap progress bar with the new percentage
$('.progress-bar').text(percentComplete + '%');
$('.progress-bar').width(percentComplete + '%');
// once the upload reaches 100%, set the progress bar text to done
if (percentComplete === 100) {
$('.progress-bar').html('Done');
}
}
}, false);
return xhr;
}
});
}
});
NODE JS 내가 행운으로, 몇 일 동안을 시도하고있다이
HTML을 할 수있는 방법. 누군가 나를 도울 수 있습니까?
https://github.com/felixge/node-formidable –
이전에 피곤했지만 사용하지 못했습니다. 내 코드에서 잘못된 방식으로 적용 할 수 있습니다. 어떻게 완료되었는지 보여줄 수 있습니까? – Illep
이 답변은 무엇입니까? http://stackoverflow.com/a/34442688/747579? –