voneFileUpload method

Future<void> voneFileUpload(
  1. BuildContext context,
  2. String filePath,
  3. ValueChanged<UploadResultEntity> changed,
  4. String parentId, {
  5. Function? progressCallback,
})

文件上传 filePath 文件路径 parentId 文件夹id changed 上传成功之后的结果

Implementation

Future<void> voneFileUpload(BuildContext context, String filePath,
    ValueChanged<UploadResultEntity> changed, String parentId,
    {Function? progressCallback}) async {

  try {
    String fileName = filePath.split("/").last;
    if(!disabledFileType(fileName)){
      return;
    }
    LoadingUtils().showLoadingDialog(context);
    Map<String, dynamic> fileMap = {};
    fileMap['file'] =
        await MultipartFile.fromFile(filePath, filename: fileName);
    FormData formData = FormData.fromMap(fileMap);
    Response response;
    Dio dio = Dio();
    dio.options.headers = {
      'userID': userId,
      'parentID': parentId,
      'secretKey': secretKey
    };
    String url = "${IPFSUtils.instance!.requestUrl}${HttpApi.fileUpload}";
    response = await dio.post<String>(url, data: formData,
        onSendProgress: (count, total) {
      if (progressCallback != null) {
        progressCallback(count, total);
      }
    });
    UploadFileEntity fileData = JsonConvert.fromJsonAsT<UploadFileEntity>(
        jsonDecode(response.data.toString()));
    print(response.data.toString());
    if (0 == fileData.code) {
      if (fileData.data != null) {
        String filePath =
            getFilePath(fileData.data!.id!, fileData.data!.fileName!);
        String coverPath = getCoverPath(fileData.data!.url!);
        coverPath = coverPath.isEmpty ? '' : Uri.decodeComponent(coverPath);
        print('coverPath$coverPath');
        print('filePath$filePath');
        UploadResultEntity entity = UploadResultEntity(filePath, coverPath,
            fileData.data!.fileName!, fileData.data!.fileSize!);
        changed(entity);
      }
    } else {
      ToastUtils.show('文件上传失败');
    }
    LoadingUtils().dismiss(context);
  } catch (e) {
    print(e);
    LoadingUtils().dismiss(context);
    ToastUtils.show('文件上传失败');
  }
}