react-native-documents / document-picker

Document Picker for React Native
https://react-native-documents.github.io/
MIT License
1.35k stars 437 forks source link

Upload documents to a server #248

Closed XEmAX32 closed 4 years ago

XEmAX32 commented 4 years ago

I'm trying to upload some files from my react native client to nodejs server using multer. If I try to upload those files using Postman it works great, but when I do this using my mobile app it gives me this error:

Error: Unexpected end of multipart data
    at /home/ema/iDiary/iDiary-server/node_modules/dicer/lib/Dicer.js:62:28
    at processTicksAndRejections (internal/process/next_tick.js:74:9)

I don't think is a server problem but this is the part of code that takes the file in:

const multer = require('multer');
const multerupload = multer({ dest: 'tmp/' })
v0.post('/upload-attachments', multerupload.any(), uploadAttachments);

and this is my react native code where I get the file using react-native-document-picker:

DocumentPicker.pick({type: [DocumentPicker.types.allFiles]}).then((res) => {
                    let split = res.uri.split('/');
                    let name = split.pop();
                    let inbox = split.pop();
                    const realPath = `${RNFS.TemporaryDirectoryPath}${inbox}/${name}`;
                    this.props.writeTaskData('attachments', {
                        filepath: realPath,
                        filename: res.name,
                        filetype: res.type,
                        name: res.name
                    });
                    console.log('docres',res)
                    this.props.navigation.navigate('Page5');
                })

and in the end this is the code where I send the files to my server:

RNFS.uploadFiles({
                    toUrl: 'https://'+address+':'+port+'/api/v0/upload-attachments',
                    files: [attachment],
                    method: 'POST',
                    headers: {
                    'Accept': 'application/json',
                    },
                    fields: {
                        homeworkId: res.id,
                        name: attachment.filename
                    },
                    begin: uploadBegin,
                    progress: uploadProgress
                })

If anyone knows the solution I would be glad to him if it would share it with me.

Elyx0 commented 4 years ago

Closing for now, please comment back if you figured it out.