react-native-fetch-blob icon indicating copy to clipboard operation
react-native-fetch-blob copied to clipboard

Download manager could not resolve downloaded file path.

Open kholiavko-roman opened this issue 7 years ago • 21 comments

Hi! Thanks for you module! While development I have one issue, when I try to download file using addAndroidDownloads

Error: Error: Download manager could not resolve downloaded file path.
Status code: undefined

Here is my code:

RNFetchBlob
    .config({
        addAndroidDownloads : {
            useDownloadManager : true,
            notification : false,
            title: 'pdfurl-guide.pdf',
            mime : 'application/pdf',
            description : 'File downloaded by download manager.'
        },
    })
    .fetch('GET', 'http://www.axmag.com/download/pdfurl-guide.pdf')
    .then((resp) => {
        console.log('DOWNLOAD complete');

        RNFetchBlob.fs.exists(resp.path())
        .then((exist) => {
            console.log(`file ${exist ? '' : 'not'} exists`)
        })
        .catch(() => {
            console.log('error while checking file for exists');
        });

        console.log(resp.path());      
    })
    .catch((errorMessage, statusCode) => {
        console.log('Error: ' + errorMessage);
        console.log('Status code: ' + statusCode);
    });

I check my path, and it return FOLDER exists

let path = RNFetchBlob.fs.dirs.DocumentDir;

RNFetchBlob.fs.exists(path)
        .then((exist) => {
            console.log(`FOLDER ${exist ? '' : 'not'} exists`)
        })
        .catch(() => {
            console.log('error while checking FOLDER for exists');
        });

Can you help me please ? Maybe I make mistake somewhere ?

kholiavko-roman avatar Aug 09 '17 14:08 kholiavko-roman

Also i trying add path to adnroidDownloads config:

let path = RNFetchBlob.fs.dirs.DocumentDir;

RNFetchBlob.config({
    addAndroidDownloads : {
        useDownloadManager : true,
        notification : false,
        path: path + '/pdfurl-guide.pdf' ,
        title: 'pdfurl-guide.pdf',
        mime : 'application/pdf',
        description : 'File downloaded by download manager.'
    },
})

in this case I recive red screen with error: Unsupported path /data/user/0/com.chat/files/pdfurl-guide.pdf

And also I trying download without addAndroidDownloads,

let path = RNFetchBlob.fs.dirs.DocumentDir;

RNFetchBlob
    .config({
        path: path + '/pdfurl-guide.pdf'
    })

In this case it downloads, return path of file, but I can not find it on the device in the com.chat/files directory it is empty. But whe I check the file for existing

RNFetchBlob.fs.exists(resp.path())
       .then((exist) => {
           console.log(`file ${exist ? '' : 'not'} exists`)
       })
       .catch(() => {
           console.log('error while checking file for exists');
       });

It return that file is exist.

What I am doing wrong ?

kholiavko-roman avatar Aug 09 '17 15:08 kholiavko-roman

@kholiavko-roman , Try to add file:// before the path.

wkh237 avatar Aug 09 '17 15:08 wkh237

@wkh237, I tried, but I still have an error.

I trying this path: file:///data/user/0/com.chat/files, also I tried to remove one slash before the data file://data/user/0/com.chat/files, but result the same - I have red screen with error Unsupported path /data/user/0/com.chat/files/pdfurl-guide.pdf

 let path = 'file://' + RNFetchBlob.fs.dirs.DocumentDir;
 RNFetchBlob
    .config({
        path: path + '/pdfurl-guide.pdf',
        addAndroidDownloads : {
            useDownloadManager : true,
            notification : false,
            path: path + '/pdfurl-guide.pdf' ,
            title: 'pdfurl-guide.pdf',
            mime : 'application/pdf',
            description : 'File downloaded by download manager.'
        },
    })
    .fetch('GET', 'http://www.axmag.com/download/pdfurl-guide.pdf')

kholiavko-roman avatar Aug 10 '17 06:08 kholiavko-roman

I found some tmp fix, If I set path to DownloadDir - I can download file without error. let path = RNFetchBlob.fs.dirs.DownloadDir + '/pdfurl-guide.pdf';

And I found that when I set DocumentDir as in prev post, with or without file:// protocol, I recive red screen and on it path different from path that I see in the console. Also I trying MainBundleDir - result the same.

Unsupported path /data/data/user/0/com.chat/files/pdfurl-guide.pdf data/data in path duplicate. I trying to set math manually without data, like this: /user/0/com.chat/files/pdfurl-guide.pdf but I also revive an error, but on this time without duplicate /data/data/

Can you help me please? I tested on real devices with android 7.1 and 6.0

kholiavko-roman avatar Aug 10 '17 06:08 kholiavko-roman

I think this is a restriction as Android Download Manager only accepts a path on external storage.

see related SO thread

wkh237 avatar Aug 10 '17 07:08 wkh237

Ok, but if I need save file into the app directory, how I can do this without download managed ? I tried, but app folder com.myapp is empty.

kholiavko-roman avatar Aug 10 '17 07:08 kholiavko-roman

You can download the file with DownloadManager API then copy the file (fs.cp) to your app folder (DocumentDir)

wkh237 avatar Aug 10 '17 07:08 wkh237

Ok, I understand. Thanks for helping!

kholiavko-roman avatar Aug 10 '17 07:08 kholiavko-roman

@wkh237, can I download file in cash directory and open it after download ? I wanna just download image in cache and open it using actionViewIntent,

I tried to do this:

RNFetchBlob
                    .config({
                       fileCache : true,
                      appendExt : 'jpg'
                    })
                    .fetch('GET', attachItem.url)
                    .then((resp) => {
                      console.log(resp);
                      console.log(resp.path());

                      RNFetchBlob.fs.exists(resp.path())
                        .then((exist) => {
                            console.log(`file ${exist ? '' : 'not'} exists`)
                        })
                        .catch(() => { console.log('err while checking') });

                      if (Platform.OS === 'ios') {
                        RNFetchBlob.ios.openDocument(resp.path());
                      } else {
                        RNFetchBlob.android.actionViewIntent('file://' + resp.path(), 'image/jpg');
                      }
                    })
                    .catch((errorMessage, statusCode) => {
                      console.log('Error: ' + errorMessage);
                      console.log('Status code: ' + statusCode);
                    });

After downloading I see the path with extension /data/user/0/com.chat/files/RNFetchBlobTmp_0boqybb710yu9tyaknbt48w.jpg and after checking on existing file result - file exists but when I opened image using default gallery app I can not see it.

The same code on ios works fine. How to save image in cache and open it ?

kholiavko-roman avatar Aug 14 '17 09:08 kholiavko-roman

If I setup path to the RNFetchBlob.fs.dirs.CacheDir, I cant open file using actionViewIntent, but if I set path to the DownloadDir, or DCIMDir I can open file via actionViewIntent.

kholiavko-roman avatar Aug 14 '17 13:08 kholiavko-roman

You can download the file with DownloadManager API then copy the file (fs.cp) to your app folder (DocumentDir)

@wkh237, also I tried to copy from download directory to DocumentDirs, but it it was not successful, document dirs are empty. ( I tried to move or copy it, and save it directry to DocumentDir or save in sub folder in DocumentDir - result the same, I see in console that file moved, but in my app directory is nothing change)

 RNFetchBlob.fs.mv(resp.path(), RNFetchBlob.fs.dirs.DocumentDir + '/test/qq.jpg')
                          .then(() => {
                            console.log('file moved');
                          })
                          .catch(() => {
                            console.log('err while moving');
                          });

kholiavko-roman avatar Aug 16 '17 10:08 kholiavko-roman

@kholiavko-roman I have the same problem, you solved it

mohamedtijani avatar Jan 18 '18 09:01 mohamedtijani

@mohamedtijani, no.

kholiavko-roman avatar Jan 18 '18 10:01 kholiavko-roman

@kholiavko-roman you still not solved problem?

romreed avatar Mar 02 '18 11:03 romreed

@romreed, unfortunately no.

kholiavko-roman avatar Mar 02 '18 11:03 kholiavko-roman

@kholiavko-roman i solved problem like this

`export function getMimeType(type) {

switch (type) {
    case "doc" :
        return "application/msword";
    case "docx" :
        return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    case "ppt":
        return "application/vnd.ms-powerpoint";
    case "pptx":
        return "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    case "xls" :
        return "application/vnd.ms-excel";
    case "xlsx" :
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    case "pdf" :
        return "application/pdf";
    case "png" :
        return "image/png";
    case "bmp" :
        return "application/x-MS-bmp";
    case "gif" :
        return "image/gif";
    case "jpg" :
        return "image/jpeg";
    case "jpeg" :
        return "image/jpeg";
    case "avi" :
        return "video/x-msvideo";
    case "aac" :
        return "audio/x-aac";
    case "mp3" :
        return "audio/mpeg";
    case "mp4" :
        return "video/mp4";
    case "apk" :
        return "application/vnd.Android.package-archive";
    case "txt" :
    case "log" :
    case "h" :
    case "cpp" :
    case "js" :
    case "html" :
        return "text/plain";
    default:
        return "*/*";
}

} export const downloadFile = (id, type, url, name) => (dispatch, getState) => { let filename = "default" var m = url.toString().match(/.*/(.+?)./); if (m && m.length > 1) { filename = m[1] }

    let dirs = RNFetchBlob.fs.dirs
    filePath = `${dirs.DownloadDir}/${filename}.${type}`

        dispatch({type: 'DOWNLOAD_FILE_BEGIN'})

        RNFetchBlob.config({
            path: filePath,
            addAndroidDownloads: {
                title: `${name} загружен`,
                description: `${name} загружен`,
                mediaScannable: true,
                mime: getMimeType(type),
                notification: true
            }
        })
            .fetch('GET', url, {
                'Cache-Control': 'no-store'
            })
            .then((res) => {
                    RNFetchBlob.android.actionViewIntent(res.path(), getMimeType(type));
                    dispatch({type: DOWNLOAD_FILE_SUCCESS, payload: {id: id, location: res.path(),mime: getMimeType(type)}})
            })
            .catch((errorMessage, statusCode) => {
                dispatch({type: DOWNLOAD_FILE_ERROR, payload: {message: errorMessage}})
          })}

` maybe this help you

romreed avatar Mar 03 '18 10:03 romreed

@romreed Thank you ! I create an account juste to say thank you ! Was blocked on this so much time...

LtkStark avatar Apr 08 '18 14:04 LtkStark

@LtkStark I'm glad that my code helped you

romreed avatar Apr 08 '18 14:04 romreed

@romreed @LtkStark Every time the control is going to catch block with the error Error: Download manager could not resolve downloaded file path. and the file is getting downloaded. I want to know the res.path() of the downloaded file. Here is my code

    const dirs = RNFetchBlob.fs.dirs;
    const filePath = `file://${dirs.DownloadDir}/${caption}.pdf`;
    const configOptions = {
      addAndroidDownloads: {
        useDownloadManager: true,
        mime: 'application/pdf',
        description: 'File downloaded by download manager.',
        path: filePath
      }
    };
    RNFetchBlob
     .config(configOptions)
     .fetch('GET', srcUrl)
     .then((res) => {
       console.log(res.path());
     })
     .catch((err) => {
         console.log(err);
      });

platform : Android

thallamsai avatar Jun 13 '18 07:06 thallamsai

removing file:// in the file path solved the issue.

thallamsai avatar Jun 13 '18 07:06 thallamsai

set the path in addAndroidDownoload, not directly in config work

let savePath = RNFetchBlob.fs.dirs.DownloadDir + '/video.mp4' RNFetchBlob .config({ fileCache: true, addAndroidDownloads: { useDownloadManager: true, notification: true, title: 'video.mp4', mime: 'video/mp4', description: 'download description', mediaScannable: true, path: savePath, } }) .fetch('GET', url) .then(res => { let downloadPath = res.path() console.log(downloadPath) }) .catch(err => { console.log(err) })

don't work ,

llet savePath = RNFetchBlob.fs.dirs.DownloadDir + '/video.mp4' RNFetchBlob .config({ fileCache: true, path: savePath, addAndroidDownloads: { useDownloadManager: true, notification: true, title: 'video.mp4', mime: 'video/mp4', description: 'download description', mediaScannable: true, } }) .fetch('GET', url) .then(res => { let downloadPath = res.path() console.log(downloadPath) }) .catch(err => { console.log(err) })

aoarashi1988 avatar Sep 20 '18 08:09 aoarashi1988