Closed 0FengQiang0 closed 1 year ago
这是我的flutter运行环境: [✓] Flutter (Channel stable, 3.7.0, on macOS 12.5 21G72 darwin-arm64, locale zh-Hans-CN) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) [✓] Xcode - develop for iOS and macOS (Xcode 14.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.3) [✓] Connected device (5 available)
上传对象我调用了 upload 方法,因为我需要一次上传多张图片,因此我在一个for循环里调用 upload,希望能上传多张图片。
类似于这样: for(PhotoSelectModel photoModel in _photoList) { upload(photoModel.imgFile.path); }
void upload( String? pickFilePath, { String? bucket, Function(dynamic data)? successBlock, Function(dynamic error)? errorBlock, }) async { try { String tmp_bucket = ""; if(bucket != null) { tmp_bucket = bucket; }else if(_bucket is String && _bucket.isNotEmpty) { tmp_bucket = _bucket; }else { await beginGetTencentCosConfigFromServer(); tmp_bucket = _bucket; } if(tmp_bucket.isEmpty) { return; }
print("拿到 bucket 啦!!!!! $tmp_bucket"); if (pickFilePath == null) { print("错误信息:请先选择需要上传的文件"); return; } //对象在存储桶中的位置标识符,即称对象键 var time = DateTime.now(); String tStr = "${time.year.toString()}${time.month.toString().padLeft(2,"0")}${time.day.toString().padLeft(2,"0")}"; var cosPath = "/img/frame/${tStr}/" + "${pickFilePath!.split("/").last}"; cosPath = "/img/frame/${tStr}/" + "${DateTime.now().microsecondsSinceEpoch}.jpg"; CosTransferManger cosTransferManger = await getTransferManger(); // 上传成功回调 successCallBack(result) { print("文件已上传到COS:$cosPath"); if(successBlock!=null) { if(result!=null) { successBlock(result); } else { String resultStr = "https://${tmp_bucket}.cos.${TestConst().PERSIST_BUCKET_REGION}.myqcloud.com" + cosPath; successBlock(resultStr); } } } //上传失败回调 failCallBack(CosXmlClientException? clientException, CosXmlServiceException? serviceException) { String error_str = ""; if (clientException != null) { error_str = clientException.toString(); print("错误信息:$error_str"); print(clientException); } if (serviceException != null) { error_str = serviceException.toString(); print("错误信息:$error_str"); print(serviceException); } if(errorBlock!=null) { errorBlock(error_str); } } //上传状态回调 stateCallback(state) { _state = state; print("上传状态回调:$_state"); } //上传进度回调 progressCallBack(complete, target) { _complete = complete; _target = target; print("上传进度回调(complete):$_complete"); print("上传进度回调(target):$_target"); } //初始化分块完成回调 initMultipleUploadCallback( String bucket, String cosKey, String uploadId) { _uploadId = uploadId; print("分块完成回调(uploadId):$_uploadId"); } //开始上传 print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 开始上传 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); _transferTask = await cosTransferManger.upload(tmp_bucket, cosPath, filePath: pickFilePath, uploadId: _uploadId, resultListener: ResultListener(successCallBack, failCallBack), stateCallback: stateCallback, progressCallBack: progressCallBack, initMultipleUploadCallback: initMultipleUploadCallback); } catch (e) { print("出错了~~:${e.toString()}"); print(e); }
}
只有其中一张图片上传成功,并且触发了 successCallBack(result) {} 其他图片应该都上传失败了,有打印日志: [Info]任务完成的回调 didCompleteWithError response = <NSHTTPURLResponse: 0x2824b3540> { URL: https://games-1302594061.cos.ap-nanjing.myqcloud.com/img/frame/20230319/1679221450034901.jpg } { Status Code: 200, Headers { Connection = ( "keep-alive" ); "Content-Length" = ( 0 ); Date = ( "Sun, 19 Mar 2023 10:24:13 GMT" ); Etag = ( "\"f11911b1f33149ad52412b85f213d822\"" ); Server = ( "tencent-cos" ); "x-cos-hash-crc64ecma" = ( 14973353465238670525 ); "x-cos-request-id" = ( NjQxNmUyY2NfYTAxNzcxMDlfOWRmYl8zZGUyOTU3 ); "x-cos-storage-class" = ( STANDARD ); } } error =
这应该是 QCloudHTTPSessionManager.m 这个文件里的一个方法打印的日志
但是我的 flutter 项目里的 failCallBack(CosXmlClientException? clientException, CosXmlServiceException? serviceException) {} 这个上传失败的回调方法没有被触发。
简而言之:就是上传多张图片,有图片上传失败了,但是失败的回调方法没有执行。
附上我的项目里上传多个对象的情形:
我检查了一下代码,是我自己调用方式的问题
我也遇到这个问题了,你怎么解决的
这是我的flutter运行环境: [✓] Flutter (Channel stable, 3.7.0, on macOS 12.5 21G72 darwin-arm64, locale zh-Hans-CN) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) [✓] Xcode - develop for iOS and macOS (Xcode 14.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.3) [✓] Connected device (5 available)
上传对象我调用了 upload 方法,因为我需要一次上传多张图片,因此我在一个for循环里调用 upload,希望能上传多张图片。
类似于这样: for(PhotoSelectModel photoModel in _photoList) { upload(photoModel.imgFile.path); }
void upload( String? pickFilePath, { String? bucket, Function(dynamic data)? successBlock, Function(dynamic error)? errorBlock, }) async { try { String tmp_bucket = ""; if(bucket != null) { tmp_bucket = bucket; }else if(_bucket is String && _bucket.isNotEmpty) { tmp_bucket = _bucket; }else { await beginGetTencentCosConfigFromServer(); tmp_bucket = _bucket; } if(tmp_bucket.isEmpty) { return; }
}
只有其中一张图片上传成功,并且触发了 successCallBack(result) {} 其他图片应该都上传失败了,有打印日志: [Info]任务完成的回调 didCompleteWithError response = <NSHTTPURLResponse: 0x2824b3540> { URL: https://games-1302594061.cos.ap-nanjing.myqcloud.com/img/frame/20230319/1679221450034901.jpg } { Status Code: 200, Headers { Connection = ( "keep-alive" ); "Content-Length" = ( 0 ); Date = ( "Sun, 19 Mar 2023 10:24:13 GMT" ); Etag = ( "\"f11911b1f33149ad52412b85f213d822\"" ); Server = ( "tencent-cos" ); "x-cos-hash-crc64ecma" = ( 14973353465238670525 ); "x-cos-request-id" = ( NjQxNmUyY2NfYTAxNzcxMDlfOWRmYl8zZGUyOTU3 ); "x-cos-storage-class" = ( STANDARD ); } } error =
这应该是 QCloudHTTPSessionManager.m 这个文件里的一个方法打印的日志
但是我的 flutter 项目里的 failCallBack(CosXmlClientException? clientException, CosXmlServiceException? serviceException) {} 这个上传失败的回调方法没有被触发。
简而言之:就是上传多张图片,有图片上传失败了,但是失败的回调方法没有执行。
附上我的项目里上传多个对象的情形:![IMG_3134](https://user-images.githubusercontent.com/16080084/226169443-3ba922ef-4a69-40ec-8c0c-c06e966285ea.PNG)