我正在嘗試在顫振中將多個影像上傳到 Rest API。我寫的代碼如下:
final List<File> _image = [];
Future<Future<bool?>?> uploadImage(filePath, url) async {
if (_image.length > 0) {
for (var i = 0; i < _image.length; i ) {
print(_image.length);
var request =
http.MultipartRequest('POST', Uri.parse(url _scanQrCode));
print(Uri.parse(url _scanQrCode));
request.files.add(http.MultipartFile.fromBytes(
'picture',
File(_image[i].path).readAsBytesSync(),
filename: _image[i].path.split("/").last
));
var res = await request.send();
var responseData = await res.stream.toBytes();
var result = String.fromCharCodes(responseData);
print(_image[i].path);
}
_submitedSuccessfully(context);
}else{
return Fluttertoast.showToast(
msg: "Please Select atleast one image",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
}
}
代碼不起作用,影像沒有上傳。請任何人幫我解決這個問題
uj5u.com熱心網友回復:
將您的代碼更改為:
final List<File> _image = [];
Future<Future<bool?>?> uploadImage(String url) async {
// create multipart request
var request = http.MultipartRequest('POST', Uri.parse(url _scanQrCode));
if (_image.length > 0) {
for (var i = 0; i < _image.length; i ) {
request.files.add(http.MultipartFile('picture',
File(_image[i].path).readAsBytes().asStream(), File(_image[i].path).lengthSync(),
filename: basename(_image[i].path.split("/").last)));
}
// send
var response = await request.send();
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
debugPrint(value);
_submitedSuccessfully(context);
});
}
else{
return Fluttertoast.showToast(
msg: "Please Select atleast one image",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
}
}
uj5u.com熱心網友回復:
這個包讓你的作業更輕松,flutter_uploader:
final uploader = FlutterUploader();
final taskId = await uploader.enqueue(
url: "your upload link", //required: url to upload to
files: [FileItem(filename: filename, savedDir: savedDir, fieldname:"file")], // required: list of files that you want to upload
method: UploadMethod.POST, // HTTP method (POST or PUT or PATCH)
headers: {"apikey": "api_123456", "userkey": "userkey_123456"},
data: {"name": "john"}, // any data you want to send in upload request
showNotification: false, // send local notification (android only) for upload status
tag: "upload 1"); // unique tag for upload task
);
uj5u.com熱心網友回復:
首先你創建變數
List<File>? imageFileList = [];
List<dynamic>? _documents = [];
當您從圖庫中選擇影像時呼叫此方法
pickMultipleImage(ImageSource source) async {
try {
final images = await picker.pickMultiImage(
maxWidth: 600, maxHeight: 600, imageQuality: 50);
if (images == null) return;
for (XFile image in images) {
var imagesTemporary = File(image.path);
imageFileList!.add(imagesTemporary);
}
} catch (e) {
}
}
當您按下按鈕將影像發送到服務器時呼叫
for(int i=0; i< _imageFileList!.length; i ){
var path = _imageFileList![i].path;
_documents!.add(await MultipartFile.fromFile(path,
filename: path.split('/').last));
}
var payload = dio.FromData.fromMap({ 'documents': _documents});
Dio() response = Dio.post(url, data: payload);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359639.html
