-1
我跟着這個例子上傳圖片到我的後端。 https://github.com/expo/image-upload-example如何將圖像從原生反應上傳到快速後端?
我在RN應用程序中使用的代碼是:
let formData = new FormData();
formData.append('name',this.state.name);
formData.append('description',this.state.description);
formData.append('price',this.state.price);
formData.append('oprice',this.state.oprice);
let fileArr = (this.state.image).split('.');
let type = fileArr[fileArr.length -1]
let uri = this.state.image
formData.append('photo',{uri, name: `${this.state.name}.${type}`, type: `image/${type}`});
console.log(formData);
AsyncStorage.getItem('jwt', (err, token) => {
fetch('http://192.168.1.83:8000/ShopRouter/deal', {
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: token,
'Content-type': false
},
body: formData
})
我用我的快遞後端代碼是:
app.post('/ShopRouter/deal', passport.authenticate('jwt2', {session:false}), function(req,res) {
upload(req, res, function(err) {
if (err) {
console.log(err);
}
console.log(req.file);
console.log(req.files);
console.log(req.photo);
console.log(req.body.name);
console.log(req.body.description);
我Multer配置爲:
var storage = multer.diskStorage({
destination: function (req, file, cb, err){
console.log(file)
if (err){
console.log(err)
}
cb(null, './uploads/')
},
filename: function (req, file, cb) {
console.log(file)
cb(null, file.originalname + '-' +Date.now())
}
});
var upload = multer({ storage: storage }).single('photo');
行console.log(文件)打印
{字段名: '照片', ORIGINALNAME: '一個空瓶子.JPG', 編碼: '7位', mime類型: '圖像/ JPEG'}
我; M不知道爲什麼後端接收這沒有圖像的URI,沒有得到保存在上傳文件夾中
req.file返回undefined。