我想上传一张图片,我正在使用http.Client()来发送请求,
static uploadImage(String id,File file) { var httpClient = createHttpClient(); Map<String,String> headers = new Map<String,String>(); headers.putIfAbsent("Authorization",() => "---"); headers.putIfAbsent("Content-Type",() => "application/json"); var body=new List(); body.add(id.) httpClient.post(URL_UPLOADIMAGE,headers: headers,body:,encoding: ) }
请求的正文和编码部分应该是什么?
解决方法
使用
MultipartRequest 级
Upload(File imageFile) async { var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead())); var length = await imageFile.length(); var uri = Uri.parse(uploadURL); var request = new http.MultipartRequest("POST",uri); var multipartFile = new http.MultipartFile('file',stream,length,filename: basename(imageFile.path)); //contentType: new MediaType('image','png')); request.files.add(multipartFile); var response = await request.send(); print(response.statusCode); response.stream.transform(utf8.decoder).listen((value) { print(value); }); }
名称空间:
import 'package:path/path.dart'; import 'package:async/async.dart'; import 'dart:io'; import 'package:http/http.dart' as http;