Upload base64 image in react native API request - reactjs

I tried to upload a base64 image cropped to 600,400 and when i do the request its tell me unexpected token '>' but when i do an upload from an image cropped 20,20 the api call works. So its a problem of length of the base64 it seems that i cannot be to large I also tried to encoded but is large too the 600,400. So i have to resign myself to send a small image or there is another way.
This is my code of the image:
ImagePicker.openPicker({
width: 600,
height: 400,
cropping: true,
includeBase64: true
}).then(image => {
uploadPictureVar = 'data:image/jpeg;base64,' + image.data;
window.picture = uploadPictureVar;
this.setState({ uploadPicture: uploadPictureVar });
});
And this is my api call
export function uploadPost(post) {
let data = {
body: post.body,
picture: post.picture,
type: post.type,
user: {
_id: post.user._id,
name: post.user.name,
picture: post.user.picture
}
}
var headers = {
'Content-Type': 'application/json',
'Access-Control-Origin': '*'
}
return fetch(URL + "/uploadPost", {
method: "post",
headers: headers,
body: JSON.stringify(data)
})
.then(response => Promise.resolve(response.json()))
.catch(err => {
return Promise.reject(err);
})
}
Thanks

Is it possible to use the Chrome inspector to see the response you're getting back from the server. Maybe the backend is throwing an error and rendering a HTML page, hence the error... Unexpected '<'. I'm guessing you're trying parse JSON but you got a HTML response instead.

I could solved it using this plugin : https://github.com/bamlab/react-native-image-resizer
This is my code:
ImageResizer.createResizedImage(window.picture, 600, 400, "PNG", 100, 0, null).then((response) => {
alert(response)
bodySendNewPost.picture = response.uri;
// response.uri is the URI of the new image that can now be displayed, uploaded...
// response.path is the path of the new image
// response.name is the name of the new image with the extension
// response.size is the size of the new image
}).catch((err) => {
alert(err)
// Oops, something went wrong. Check that the filename is correct and
// inspect err to get more details.
});

Related

Can't send image to server, getting "Error: Request failed with status code 500" while sending image file by axios in react native project

I am using react native RNCamera. I am getting the file uri from the data retuned by react native RNCamera
Here is the code how I did that
const mimeType = mime.getType(damageImage.uri);
console.log('received image', damageImage.uri, mimeType);
//Here I am getting damageImage.uri as file:///data/user/0/com.tvsmobile/cache/Camera/2c32593e-5015-4ef1-b8b0-db141628444e.jpg
// and mime type: image/jpeg
try {
const formData = new FormData();
formData.append('Content-Type', 'image/jpeg');
formData.append('damage_image', {
uri: Platform.OS === 'android' ? damageImage.uri : damageImage.uri.replace('file://', ''),
name: 'image.jpg',
type: mimeType, //I tried by passing 'image/jpeg' here
});
let response = await axios.request({
baseURL: base_url,
method: 'post',
url: headRoute + transactionRoute + damageRoute + 'add/',
headers: {
'accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
data: JSON.stringify(formData),
});
return response.data;
} catch (e) {
console.log('error at sending damage asset:' + e);
const response = e.response;
switch (response.status) {
case 601:
case 605:
case 606:
Snackbar.show({text: response.data.errMessage});
break;
default:
Snackbar.show({text: defMsg});
break;
}
}
Any help will appreciate
Thanks in advance
Error 500 means an error has happened on the server side (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500), so by inspecting front-end code is rather difficult, if not impossible, to say what is the cause. If you are able, you should check the server logs to find the cause, which might or might not be caused by your data.

Get the image from the server

I uploaded an image file using axios post method and it was successfully uploaded to the server...
On return to the POST method, it returned the Image URL as:
{imageUrl: "/root/TTAppJava/images/players/Screenshot from 2020-11-24 16-38-57.png"}
Now, I want to display the image onto my screen.
How can I get that image?
I am using React.js to implement this.
The URL I used to post the image is:
http://139.59.16.180:8269/player/details/${id}
I am doing this to upload my data:
var formData = new FormData()
formData.append("file", image);
const theWrapper = {
"data": {
"name": name,
"age": age,
"email": email,
"gender": gender
}
}
formData.append("theWrapper", JSON.stringify(theWrapper))
Axios.post("http://139.59.16.180:8269/player/add",
formData,
{ headers: { Authorization: "Bearer " + localStorage.getItem("token") } }
).then(res => {
console.log(res)
alert("Player added successfully.")
history.push("/players")
})
.catch(err => alert(err.messge))
And the response I am getting is:
{id: 14, name: "shreyas", email: "asdfjka#gmail.com", gender: "Male", imageUrl: "/root/TTAppJava/images/players/Screenshot from 2020-11-24 16-38-57.png", …}
I will give you an example how its done in Node app, since the underlying concept will be the same as I am not a Java developer.
First please note your image_url/download_url should be saved as follows,
https://yourdomain/folder/image_name
example: http://localhost:3000/uploads_folder/my_image_name.jpg
and then you need a route in Java Spring which figures out what image to send to the front-end as Stream like below,
router.get("/:folder/:image_name", async (req, res) => {
const { folder, image_name } = req.params;
// find an image based on the downloadUrl in the folder where files are saved.
// Please note, after uploading file successfully generate a download url
// appropriately so that this route can help figure out which image you
// are looking for.
const file = path.join(`path/to/${folder}`, `${image_name}`);
// Figure out a way how stream works in your context.
// Providing MIME type is important!
const type = mime[path.extname(file).slice(1)] || "image/jpg";
const s = fs.createReadStream(file);
s.on("open", () => {
res.set("Content-Type", type);
s.pipe(res);
});
s.on("error", (err) => {
// Handle Error
});
});

Image upload params in react froala wysiwyg-editor

I was used ReactJS version of Froala WYSIWYG Editor. When I use Image Upload feature, I can't get the params at server request.
This is config:
this.config = {
// Set the image upload parameter.
imageUploadParam: 'image',
// Set the image upload URL.
imageUploadURL: apiUrl + "/api/v1/admin/upload/image",
// Additional upload params.
imageUploadParams: {
token: cookie.getItem('token'),
test_id: '11',
},
// Set request type.
imageUploadMethod: 'POST',
// Set max image size to 2MB.
imageMaxSize: 2 * 1024 * 1024,
// Allow to upload PNG and JPG.
imageAllowedTypes: ['jpeg', 'jpg', 'png', 'gif'],
}
When uploading an image I receive the following message:
{"code":403,"message":"Token is not valid!"}
I checked the request entry:
console.log(request.body);
Result: {}
console.log(request.query);
Result: {}
console.log(request.params);
Result: {}
Did I miss something or is the Config section wrong?
Junaid Babatunde wrote a great article on this, Here is the link:
https://medium.com/#junaidtunde1/angular-2-with-froala-text-editor-image-upload-to-remote-server-732ef2793356
The default image upload is intercepted with a 'beforeUpload' event where you can send it to the database and then in the callback insert the image (from the database) into the editor, the original copy is thrown away after being sent to the database which is the copy that is then inserted in to the editor.
By the way - imageUpload: true is obviously needed!
Here is the code:
options: Object = {
charCounterCount: false,
placeholderText: 'Edit Your Content Here!',
imageUpload: true,
imageDefaultAlign: 'left',
imageDefaultDisplay: 'inline-block',
// Set max image size to 5MB.
imageMaxSize: 5 * 1024 * 1024,
// Allow to upload PNG and JPG.
imageAllowedTypes: ['jpeg', 'jpg', 'png'],
events: {
'froalaEditor.image.beforeUpload': function(e, editor, images) {
// Before image is uploaded
const data = new FormData();
data.append('image', images[0]);
axios.post('your_imgur_api_url', data, {
headers: {
'accept': 'application/json',
'Authorization': 'your_imgur_client_id/api_key',
'Accept-Language': 'en-US,en;q=0.8',
'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
}
}).then(res => {
editor.image.insert(res.data.data.link, null, null, editor.image.get());
}).catch(err => {
console.log(err);
});
return false;
}
}
};
I wrote two articles on this worth checking out: https://ryan-mckenna.medium.com/froala-events-in-react-typescript-58e7e8ff1d4f and
https://ryan-mckenna.medium.com/froala-editor-react-scroll-bar-start-position-3f6d788c8768
Also shows how to integrate with React and events!
Not sure how your server side work , but in case you want the token in the header like Bearer header you pass it in the requestHeaders option
requestHeaders: {
Authorization: `Bearer ${token}`
},

Can't get the data response object from react-native-customized-image-picker

I have read in the document here: https://github.com/mg365/react-native-customized-image-picker that I could get the response object of data from an image, but when I tried it I got undefined. I can get other objects like size, mime, and path like this:
SelectPhoto = () =>{
ImagePicker.openPicker({
cropping: true,
title: 'Select a Picture',
isCamera: true,
}).then((imgResponse) => {
console.log(imgResponse[0].path); <-- This logs the correct path
console.log(imgResponse[0].data); <-- This logs the undefined
let imgSource = { uri: imgResponse[0].path };
this.setState({
userimgSource: imgSource,
});
});
}
My ultimate goal is to upload an image to a server and I think having the data is required? I am using the RNFetchBlob multipart/form-data. Any help on this is appreciated, thank you!
First of all data prop is not at all required, you can upload your file using path. If you really want data then there is a prop name "includeBase64"(by default it false) set it to true.
SelectPhoto = () =>{
ImagePicker.openPicker({
cropping: true,
title: 'Select a Picture',
isCamera: true,
includeBase64:true
})
NOTE: Setting {includeBase64: true} will convert your image to base64 which may lead to OUT OF MEMORY issue in android when you try to upload a large image.
To Upload Your using path and React-Native-Fetch-Blob
RNFetchBlob.fetch('POST', URL, {
// dropbox upload headers
...
'Content-Type': 'multipart/form-data',
// Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
// Or simply wrap the file path with RNFetchBlob.wrap().
}, [
// element with property `filename` will be transformed into `file` in form data
{ name: 'file', filename: 'Image.png', data: RNFetchBlob.wrap(response.uri) },
// custom content type
]).then((res) => {
})
.catch((err) => {
// error handling ..
})
And if you have RN>=0.54 then You don't need fetch-blob to upload image React-native now has full blob support.try this
var photo = {
uri: URI,
type:image/png, // check your object you will get mime-type in image picker response.
name: file,
fileName:Image.png
};
var body = new FormData();
body.append('file', photo);
axios({
method: 'post',
url: 'URL',
data: body,
config: { headers: {'Content-Type': 'multipart/form-data' }}
})
reference Links
upload video using React-native-fetch-blob
react-native blob support announcement
send Form data using Axios

froalaEditor.image.uploaded and redux

I'm trying to use the custom upload functionality for images in froala so have setup the following:
imagePaste: true,
imageUploadParam: 'file',
imageUploadParams: {
froala: true,
},
imageUploadURL: '/update/files',
imageUploadMethod: 'POST',
This successfully uploads the image to our server when an image is pasted in.
We need to rewrite the reponse we get from the server to build the url so have the following:
'froalaEditor.image.uploaded' : (e, editor, response) => {
var imageData = null;
response = JSON.parse(response);
fetch(
"?partial=editorimage.json&type="+response[0].documentId,
{
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
).then(response => response.json())
.then(json => {
imageData = json.authenticatedId;
editor.image.insert(imageData, true, {'remedyTag': 'yes'}, editor.image.get(), {link:imageData});
}
}));
}
The image is posted ok into the editor for the user to see and if I inspect the element it has my re-written URL so all good.
<img class="fr-dib fr-error fr-draggable"
src="http://server:8080/2018/02/06/807d2a29-a9df-4d38-ad44-b6970d82462e?expiration=1517925153113&key=6IAFCT0&signature=89-sDmKsYMW3Ylktft5mUu6YwnY="
style="width: 300px;" data-remedytag="yes">
The problem is that our redux state only has a tag for the inserted image:
update(pin): null
editorState(pin): "<p><br></p>"
I have a saga attached to the onModelChange item in the FroalaEditor definiton which is firing fine for text.
If I manpulate the image (e.g. add a caption) then the state in redux then has the
update(pin): null
editorState(pin): "<p><span class="fr-img-caption fr-dib fr-error" style="width: 300px;"><span class="fr-img-wrap" style="width: 300px;"><img src="http://server:8080/2018/02/06/807d2a29-a9df-4d38-ad44-b6970d82462e?expiration=1517925153113&key=6IAFCT0&signature=89-sDmKsYMW3Ylktft5mUu6YwnY=" data-remedytag="yes"><span class="fr-inner">123<br><br></span></span></span></p>"
Any ideas?!
The problem was our server wasn't responding with {"link":"path/to/image"} I was hoping we could take the response and rework in the uploaded event, but it needs to be done before then - we're going to do that server side

Resources