我是从image_picker包中获取图片的。^0.6.6+1
我试图将flutter中的http.MultipartRequest中的图片上传到node服务器,但它无法识别图片。
当我从postman发送以formdata为图片的POST请求时,图片被上传了。
下面是代码
事件.飞镖
import 'dart:io';
import 'package:flutter/material.dart';
import './event.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<void> addEvents(Event event, File uploadPoster) async {
final url = 'http://10.0.2.2:8080/event/event';
String fileName = uploadPoster.path.split("/").last;
var request = http.MultipartRequest('POST', Uri.parse(url));
request.files
.add(http.MultipartFile(
'image',
uploadPoster.readAsBytes().asStream(),
uploadPoster.lengthSync(),
filename: fileName
));
request.fields['titleOfTheEvent'] = event.title;
request.fields['startingDateOfTheEvent'] = event.eventStartDate;
request.fields['endingDateOfTheEvent'] = event.eventEndDate;
request.fields['venue'] = event.venue;
request.fields['industry'] = event.industry;
request.fields['numberOfStalls'] = event.numberOfStalls;
request.fields['stallBookingContactNumber'] = event.contactNumber;
request.fields['description'] = event.description;
request.fields['ticketSaleLink1'] = event.ticketSaleLink1;
request.fields['ticketSaleLink2'] = event.ticketSaleLink2;
request.fields['ticketSaleLink3'] = event.ticketSaleLink3;
request.fields['eventLinkFacebook'] = event.facebookEventLink;
request.fields['eventLinkInstagram'] = event.instagramEventLink;
request.fields['token'] = authToken;
var res= await request.send();
}
app.js
const express = require('express');
const bodyparser = require('body-parser');
const mongoose = require('mongoose');
const multer = require('multer');
const path = require('path');
const eventRoutes = require('./routes/eventRouter');
const companyRoutes = require('./routes/companyRouter');
const authrouter = require('./routes/authrouter');
const exhibitorrouter = require('./routes/exhibitorrouter');
const stallsrouter = require('./routes/stallrouter');
const app = express();
app.use(bodyparser.json());
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
})
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './images/');
},
filename: (req, file, cb) => {
cb(null, file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'image/png' ||
file.mimetype === 'image/jpg' ||
file.mimetype === 'image/jpeg'
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single('image')
);
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use('/auth', authrouter);
app.use('/event', eventRoutes);
app.use('/company', companyRoutes);
app.use('/exhibitor', exhibitorrouter);
app.use('/stalls', stallsrouter);
app.use((error, req, res, next) => {
console.log('App ' + error);
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(status).json({ error: message, data: data });
});
mongoose.connect('mongodb://localhost:27017/scope').then(result => {
app.listen(8080)
}).catch(err => console.log(err));
下面是用flutter上传图片到节点服务器的示例代码。
https:/gist.github.comchunhunghan7711ca0f6a0e9cb3d41f5b50d2d857ba。
不然就试试flutter dio包 (https:/pub.devpackagesdio。)