- 这里是我使用http上传到服务器的函数。我尝试将图像转换为base64字符串,但问题仍然存在 *
uploadImage() async {
var postUri = Uri.parse('${xyz}/api/v1/listing/store');
var request = new http.MultipartRequest("POST", postUri);
request.fields['name'] = titleTEC.text.toString();
request.fields['category_id'] = selectedCategoryId.toString();
request.fields['price'] = priceTEC.text.toString();
request.fields['discount'] = 2.toString();
request.fields['discount_type'] = 'percent';
request.headers['moduleId'] = 2.toString();
Uint8List _list = await singleImage.readAsBytes();
request.files.add(http.MultipartFile(
'image', singleImage.readAsBytes().asStream(), _list.length,
filename: '${DateTime.now().toString()}.png',
));
// request.files.add(new http.MultipartFile.fromString('image', base64Image));
request.send().then((response) {
print(response.statusCode);
});
}
4条答案
按热度按时间mznpcxlj1#
你可以使用这个代码,它的工作完美
}
hmae6n7t2#
查看堆栈跟踪,似乎您正在调用
dart:io
代码,这在Web上不受支持。您确定sixam_mart
代码设计为在浏览器中工作吗?z4iuyo4d3#
使用dio包,它也用于API调用,它的效果很好。
e37o9pze4#
使用路径添加图像文件
request.files.add(await http.MultipartFile.fromPath('image',filepath));