用AJAX上传文件到express失败
发布于 2 个月前 作者 CarlosRen 240 次浏览 来自 问答

上传文件失败,原因是?

// html
 <form id="addOperationInfo"  role="form" class="">
 	<div class="form-group form-control">
 		<label for="addOperationDetails" class="col-md-4 rightlabel">回复信息</label>
 		<input id="addOperationDetails" type="text" class=" col-md-6"  placeholder="请输入详细说明">
 	</div>
 	<div id="fileFrom" class="form-group form-control">
 		<label for="addOperationFile" class="col-md-4 rightlabel">上传附件</label>
 		<input id="addOperationFile" type="file" class=" col-md-6" >
 	</div>
 </form>

//  js
var formData = new FormData();
    formData.append('file', $('#addOperationFile')[0].files[0]);
    console.log(formData);
    $.ajax({
        url: serverip + '/upload',
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
    }).done(function(res) {
        console.log(res);
    }).fail(function(err) {
        console.log(err);
    });


//express app.js
app.use('/upload', function(req,res,next){
  console.log(req.body);  //undefined
  console.log(req.files);  //undefined
});
2 回复

少了bodyParser

@iamcc express里要把bodyParser修改了嘛?

回到顶部