在上传到服务器之前,我需要调整图片大小。
我正在使用 io 和 image 包。
import 'dart:io';
import 'package:image/image.dart' as Img;
使用此函数
uploadImages(File image_File) async {
Img.Image image_temp = Img.decodeImage(image_File.readAsBytesSync());
Img.Image resized_img = Img.copyResize(image_temp, 800);
File resized_file = File('resized_img.jpg')
..writeAsBytesSync(Img.encodeJpg(resized_img));
var stream = new http.ByteStream(DelegatingStream.typed(resized_file.openRead()));
var length = await resized_file.length();
var uri = Uri.parse("https://myserver.com/upload.php");
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile('file', stream, length,
filename: p.basename("resized_image.jpg"));
request.files.add(multipartFile);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
当我运行这段代码时,应用程序会冻结,图片也没有上传到服务器。