简体   繁体   English

Flutter 上传带进度条的大文件。 App和Web都支持

[英]Flutter upload large file with progress bar. App and Web both support

I am stuck in issue with upload large file.我遇到上传大文件的问题。 A flutter application compiled for Web and App (Native) when file upload from app is working fine but web it hanged.当从应用程序上传文件工作正常但 web 挂起时,为 Web 和应用程序(本机)编译了 flutter 应用程序。 how to send large file as stream request.如何将大文件作为 stream 请求发送。

I am new in flutter and working on current existing app which has a feature to upload large tutorial video files and PDF files.我是 flutter 的新手,正在开发当前现有的应用程序,该应用程序具有上传大型教程视频文件和 PDF 文件的功能。 requirement is to show the progress bar during the upload the file, currently app has I used dio but it hanged in web version and not file not upload operation going failed.要求是在上传文件的过程中显示进度条,目前应用程序有我使用dio但它挂在 web 版本中,而不是文件不上传操作失败。

  • File size approximate 400MB to 700MB文件大小大约 400MB 到 700MB

Currently using following packages目前使用以下软件包

dependencies:
  http: ^0.12.2
  dio: ^3.0.10

Could you please help me out for this issue?你能帮我解决这个问题吗?

I am trying to achieve with below code but some how it not working.我正在尝试使用以下代码来实现,但有些代码无法正常工作。 https://github.com/salk52/Flutter-File-Upload-Download https://github.com/salk52/Flutter-File-Upload-Download

It's thrown an error like "Memory buffer allocation failed " , I am unable to update the version of dio or http due to other package goes disturb.它抛出类似“内存缓冲区分配失败”的错误,由于其他 package 干扰,我无法更新dio或 http 的版本。 somehow I have to achieve it using httpclient or dio .不知何故,我必须使用httpclientdio来实现它。 I could not update the version for package because it messed up other package dependency.我无法更新 package 的版本,因为它弄乱了其他 package 依赖项。

Sample ref.样品参考。 code as below: File size approximate around 500 MB to 700MB代码如下:文件大小约为 500 MB 到 700MB

For ref.对于参考。 following code which using in code.以下代码中使用的代码。

Dio package example: Dio package 示例:

#Dio example start

  Future<NormalResponse> addSubjectMaterial(
      {GetSubjectMaterial objSubMat,
      bool isDelete,
      PlatformFile objfile,
      Function sendProgress,
      Function receiveProgress,
      dio.CancelToken cancelToken}) async {
    NormalResponse objRes = NormalResponse();
    try {
      print(objSubMat.objMaterial.subjectId);
      dio.FormData formData = dio.FormData();
      formData.fields.add(MapEntry("ObjSubMat", json.encode(objSubMat)));
      formData.fields.add(MapEntry("IsDelete", isDelete.toString()));
      formData.fields
          .add(MapEntry("ClassesId", AppConstants.classesId().toString()));
      if (objfile != null) {
        formData.files.add(
            MapEntry("objfile", await getMultipartFile(objfile, "objfile")));
      }
      var resp = await dio.Dio().post(
        AppConstants.addUpdateSubjectMaterial,
        data: formData,
        options: requestConfig,
        cancelToken: cancelToken,
        onSendProgress: sendProgress,
        onReceiveProgress: receiveProgress,
      );

      // String respStr = resp.toString();
      // objRes = NormalResponse.fromJson(json.decode(respStr));
      objRes = NormalResponse.fromJson(resp.data);
    } catch (err) {
      objRes.err = err.toString();
      objRes.isSuccess = false;
      objRes.newId = -1;
      sendProgress = null;
      receiveProgress = null;
    }
    return objRes;
  }

#Dio example end

#httpclient example code is there any solution with progress bar in this sample code.

   Future<NormalResponse> addUpdateSubjectMaterialHttp(
       {GetSubjectMaterial objSubMat,
       bool isDelete,
       PlatformFile objfile,
       Function sendProgress,
       Function receiveProgress,
       dio.CancelToken cancelToken}) async {
     NormalResponse objRes = NormalResponse();
     try {
       var req = http.MultipartRequest(
         "POST",
         Uri.parse(AppConstants.addUpdateSubjectMaterial),
       );
       req.headers.addAll({
         'Content-type': 'application/json',
         'Accept': 'application/json',
       });
       req.fields['ObjSubMat'] = json.encode(objSubMat);
       req.fields['IsDelete'] = isDelete.toString();
       req.fields['ClassesId'] = AppConstants.classesId().toString();

       if (objfile != null) {
         req.files.add(http.MultipartFile(
             "objFile", objfile.readStream, objfile.size,
             filename: objfile.name));
       }

       var resp = await req.send();
       String result = await resp.stream.bytesToString();
       objRes = NormalResponse.fromJson(json.decode(result));
       print(objRes.isSuccess);
       print(objRes.err);
       print("Here done");
     } catch (err) {
       print(err);
       throw err;
     }
     return objRes;
   }
   
#httpclient


Http package example:

#example start

  Future<NormalResponse> addSubjectMaterial(
      {GetSubjectMaterial objSubMat,
      bool isDelete,
      PlatformFile objfile,
      Function sendProgress,
      Function receiveProgress,
      dio.CancelToken cancelToken}) async {
    NormalResponse objRes = NormalResponse();
    try {
      print(objSubMat.objMaterial.subjectId);
      dio.FormData formData = dio.FormData();
      formData.fields.add(MapEntry("ObjSubMat", json.encode(objSubMat)));
      formData.fields.add(MapEntry("IsDelete", isDelete.toString()));
      formData.fields
          .add(MapEntry("ClassesId", AppConstants.classesId().toString()));
      if (objfile != null) {
        formData.files.add(
            MapEntry("objfile", await getMultipartFile(objfile, "objfile")));
      }
      var resp = await dio.Dio().post(
        AppConstants.addUpdateSubjectMaterial,
        data: formData,
        options: requestConfig,
        cancelToken: cancelToken,
        onSendProgress: sendProgress,
        onReceiveProgress: receiveProgress,
      );

      // String respStr = resp.toString();
      // objRes = NormalResponse.fromJson(json.decode(respStr));
      objRes = NormalResponse.fromJson(resp.data);
    } catch (err) {
      objRes.err = err.toString();
      objRes.isSuccess = false;
      objRes.newId = -1;
      sendProgress = null;
      receiveProgress = null;
    }
    return objRes;
  }

#example end

You must use Future, await and async which continues the task of streams in background thread while the UI of your application works smoothly.您必须使用 Future、await 和 async,它们在后台线程中继续流的任务,而您的应用程序的 UI 工作顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM