Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

文件上传问题

$
0
0

**1.**HTML form表单提交

<form action="/" method="POST" enctype="multipart/form-data">
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="image" name="imgfile">
    <p class="help-block">text here.</p>
  </div>
  <button type="submit" id="submit" class="btn btn-default">Submit</button>
</form>
后台没有接收到file

2.$.ajax()

前端代码:
let formdata = new FormData()
formdata.append('file', $('#image')[0].files[0])
$.ajax({
  url: '/',
  type: 'post',
  data: formdata,
  processData: false,
  success: function (result) {
    console.log(result)
  }
})
后台代码:
const formidable = require('formidable');
router.post('/', checkLogIn, function (req, res, next) {
	let form = new formidable.IncomingForm()
	form.parse(req, function(err, fields, files){
		console.log(files)
		res.send('///////////////')
	})
})
后台也没有接收到files

3. xhr

前端代码:
	let formdata = new FormData()
	formdata.append('file', $('#image')[0].files[0])
	let xhr = new XMLHttpRequest()
	xhr.open('post', '/poolimg', true)
	xhr.send(formdata)
	xhr.onload = function() {
		console.log(xhr.response)
	}
后台代码:
const formidable = require('formidable');
router.post('/', checkLogIn, function (req, res, next) {
	let form = new formidable.IncomingForm()
	form.parse(req, function(err, fields, files){
		console.log(files)
		res.send('///////////////')
	})
})

后台能接收到files

为什么会这样呢?


Viewing all articles
Browse latest Browse all 14821

Trending Articles