react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

progress not work when downloadFile

Open xclidongbo opened this issue 7 years ago • 10 comments

version: "react-native-fs": "2.13.3" platform: iOS

code:

let documentPath = RNFS.DocumentDirectoryPath; 
    let zipPath = documentPath+'/app.zip'
    let options = {
      fromUrl: downLoadUrl,
      toFile: zipPath,
      background: true,
      progressDivider: 5,
      discretionary: true,
      cacheable: true,
      begin: (res) => {
        console.log('begin', res);
      },
      progress: (res) => {
        console.log('progress', res)
      }
    }
    let ret = RNFS.downloadFile(options);
    ret.promise.then(res=>{
      console.log('====================================');
      console.log('complete',res);
      console.log('====================================');
    }).catch(err=>{
      console.log(err);
    })

xclidongbo avatar Sep 18 '19 07:09 xclidongbo

问题找到了, iOS原生代码,Downloader.m文件中,有判断,如果[_statusCode isEqualToNumber:[NSNumber numberWithInt:200]]才会进入进度更新的回调. 其它一概不接受.我们服务端返回的是_statusCode 等于 201.

xclidongbo avatar Sep 23 '19 02:09 xclidongbo

I think it is necessary to fix this problem.

xclidongbo avatar Sep 25 '19 13:09 xclidongbo

@xclidongbo it looks like when I provide begin callback together with progress (even when begin is noop function), then progress is working correctly again.

Can you check?

sebqq avatar Nov 15 '19 13:11 sebqq

@sebinq you saved my day, thanks!

oisu avatar Dec 06 '19 14:12 oisu

mine too! thanks @sebinq but funny, I remember this working without begin

LuisRodriguezLD avatar Jan 08 '20 02:01 LuisRodriguezLD

@LuisRodriguezLD yep, it was working before just fine. I don't know what what has changed :D But it is good that there is this hack which make it works again.

sebqq avatar Jan 08 '20 08:01 sebqq

@sebqq Thanks !, Saved my evening :-p

Charlicus avatar Apr 15 '21 17:04 Charlicus

Glad to help 🍻

sebqq avatar Apr 15 '21 18:04 sebqq

Thanks @sebqq for your awesome help.

I don't know why still it isn't fixed, by using a default noop function this issue would be fixed.

amerllica avatar Nov 16 '22 11:11 amerllica

Facing the same issue right now with progress not working with uploading files. Can anyone please help. I have added the begin callback as well:

const fields = {
            "x-goog-date": credentials.x_goog_date,
            "success_action_status": credentials.success_action_status,
            "x-goog-algorithm": credentials.x_goog_algorithm,
            "content-encoding": credentials.content_encoding,
            "x-goog-credential": credentials.x_goog_credential,
            "acl": credentials.acl,
            "key": credentials.key,
            "x-goog-signature": credentials.x_goog_signature,
            "policy": credentials.policy,
            "x-goog-meta-uid": credentials.x_goog_meta_uid,
            "x-goog-meta-filename": credentials.x_goog_meta_filename,
        };
        try {
            const uploadOptions = {
                toUrl: url,
                files: [{
                    name: 'file',
                    filename: filename,
                    filepath: imageData.sourceURL.replace('file://', ''),
                    filetype: imageData.mime,
                }],
                method: 'POST',
                fields: fields,
                begin: (response: any) => {
                   console.log('upload started')
                },
                progress: (response: any) => {
                    console.log('response.totalBytesSent: ', response.totalBytesSent);
                    console.log('response.totalBytesExpectedToSend: ', response.totalBytesExpectedToSend);
                }
            };
            const result = await RNFS.uploadFiles(uploadOptions).promise;
        } catch (error: any) {
            console.log('Error: ', error.message, error.response?.data);
        }

upload started is logging fine but the progress callback is not being triggered. Does anyone know the solution I have exhausted everything.

Edit: File upload is also being done successfully. No errors in that.

raahimkhan avatar Sep 24 '24 14:09 raahimkhan