Android 多部分标题中的ContentType和颤振中的Dio中的ContentType
我一直在尝试将图像和数据上传到服务器,并使用了Android 多部分标题中的ContentType和颤振中的Dio中的ContentType,android,ios,flutter,content-type,multipart,Android,Ios,Flutter,Content Type,Multipart,我一直在尝试将图像和数据上传到服务器,并使用了httpmultipart和Dio两种形式,但服务器响应502错误。根据我的研究,这是因为我在标题中提供了内容类型。我尝试了很多方法,但仍然没有成功 Dio码 Map<String, String> headers = new Map<String, String>(); headers.putIfAbsent("Authorization", () =>token); headers
httpmultipart
和Dio
两种形式,但服务器响应502
错误。根据我的研究,这是因为我在标题中提供了内容类型。我尝试了很多方法,但仍然没有成功
Dio码
Map<String, String> headers = new Map<String, String>();
headers.putIfAbsent("Authorization", () =>token);
headers.putIfAbsent("Content-Type", () => "multipart/form-data");
try {
Dio dio = Dio();
dio.options.contentType = Headers.formUrlEncodedContentType;
FormData formData = new FormData.fromMap({
"file": await MultipartFile.fromFile(_image.path,
filename: "tshirt_view_kovi.jpg"),
'name': fields.companyName,
'type': type,
'contactId': fields.companyPhone,
'taxId': fields.taxId,
'lat': "90",
'long': "180",
'address': fields.companyAddress,
'title': fields.representativeTitle,
'representative': fields.representativeName,
'state': fields.state,
'city': fields.city,
'zip': fields.zip,
});
Response response = await dio.post(
"https://xxxxxxxxx.com/v1/account/business/create",
data: formData,
options: Options(headers: headers));
Log.e("Response",response);
} catch (e) {
print(e);
}
使用MultipartFile发布内容时,请删除标题选项中的“内容类型”(=>“多部分/表单数据”)
确保您的标题仅使用授权
Map<String, String> headers = new Map<String, String>();
headers.putIfAbsent("Authorization", () => token);
headers.putIfAbsent("Content-Type", () => "multipart/form-data");
Map<String, String> body = {
'name': fields.companyName,
'type': type,
'contactId': fields.companyPhone,
'taxId': fields.taxId,
'lat': "90",
'long': "180",
'address': fields.companyAddress,
'title': fields.representativeTitle,
'representative': fields.representativeName,
'state': fields.state,
'city': fields.city,
'zip': fields.zip,
};
Log.e("URL", url);
Log.e("URL", body);
Log.e("TOken", token);
var uri = Uri.parse(
"https://xxxxxxxxx.com/v1/account/business/create");
var request = new http.MultipartRequest("POST", uri);
if (_image != null) {
//var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
//var length = await _image.length();
//var multipartFile = new http.MultipartFile('imageCover', stream, length, filename: _image.path);
//request.files.add(multipartFile);
request.files.add(
http.MultipartFile(
'imageCover',
File(_image.path).readAsBytes().asStream(),
File(_image.path).lengthSync(),
filename: _image.path
.split("/")
.last
)
);
}
if (logoimage != null) {
// var stream = new http.ByteStream(DelegatingStream.typed(logoimage.openRead()));
// var length = await logoimage.length();
// var multipartFile = new http.MultipartFile('imageLogo', stream, length, filename: logoimage.path);
// request.files.add(multipartFile);
request.files.add(
http.MultipartFile(
'imageLogo',
File(_image.path).readAsBytes().asStream(),
File(_image.path).lengthSync(),
filename: _image.path
.split("/").last
)
);
}
request.fields.addAll(body);
request.headers['Authorization'] = token;
request.headers['Content-Type'] = 'multipart/form-data';
request.send().then((response) {
print(response.statusCode);
});