How to send multiple data at once using Fetch Blob?

I am using the response-native-fetch-blob module to handle image loading, currently I am only passing image data to the POST method, but is there any way to extend this by passing a custom data object?

On the server side, it reads the image database64, I crop and resize the image and return the image URL to my application. With this image url, I have to do another HTTP POST to the server with the form data + image url I just received.

I was wondering if it was possible to send all the data (including image data) at once, instead of doing two separate POST requests.

Here is the code:

uploadFile([ { name: 'uploaded_picture', type: 'image/png', filename: 'picture.png', data: this.state.imageUploadData } ]) 

This is the implementation of uploadFile, which gives you an example on the module page.

 import RNFetchBlob from 'react-native-fetch-blob'; Constants = require('../utils/constants'); let uploadFile = (data) => { return RNFetchBlob.fetch('POST', Constants.UPLOAD_URL, { Authorization : "Bearer access-token", 'Content-Type' : 'multipart/form-data', }, data); } module.exports = uploadFile; 

And here I am processing the server side request:

 app.route('/upload') .post(function (req, res, next) { var fstream; var serverObject = server.address(); var serverUrl = serverObject.address + ':' + serverObject.port; console.log(req); req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file, filename) { console.log("Uploading: " + filename); //Path where image will be uploaded var filename = new Date().getTime() + '_' + filename; fstream = fs.createWriteStream(__dirname + '/public/images/' + filename); file.pipe(fstream); fstream.on('close', function () { console.log("Upload Finished of " + filename); gm(__dirname + '/public/images/' + filename) .resize('400', '300', '^') .gravity('Center') .crop('400', '300') .write(__dirname + '/public/images/' + filename, function(err) { if (!err) { console.log('Resized image successfully: ' + filename); res.json( { image_url: 'http://' + serverUrl + '/images/' + filename }); // Instead of sending a response back with image URL, I would like to save my formData directly here. } }); }); }); } ); 

Any suggestion on how I can achieve this with FetchBlob? I know I can transfer multiple files / data because it is an array, but I will no longer have access to this imageURL, right? So that would not fit my needs.

Thanks in advance.

0
source share

Source: https://habr.com/ru/post/1244380/


All Articles