Forum Discussion

irfansayyed101's avatar
irfansayyed101
Contributor
8 years ago

Facing issue with image upload API

Hi ,

We are developing separate Android Mobile app for a Lithium community.

Here we need to upload an image . There are two way you can upload an image but attachment and by inserting the photos . 

Here we need to insert the photos for which we are using following api,

 

/restapi/vc/users/self/media/albums/default/public/images/upload?restapi.session_key=${the_key}

 

but its giving the error "<error code="100"> <message> The file upload encountered a problem. </message>" 

 

For reference here is the code , just change the username and password on your respective stage instance.

For this create a custom component and add that into custom page and try this. 

 

<form id="myForm" enctype="multipart/form-data">
image: <input name="image" id="my_image" type="file" value="browse"> </input>
<input type="submit" value="Submit form" />
</form>

<#assign the_key = rest("authentication/sessions/login?user.login={username}&user.password={password}").value />

<#assign your_endpoint_url = "/restapi/vc/users/self/media/albums/default/public/images/upload?restapi.session_key=${the_key}" />

<@liaAddScript>
(function($) {
$(document).ready(function(){
$("#myForm").submit(function(e){
var form_data = new FormData(this);
console.log(form_data);
console.log('get image by name');
console.log(form_data.get("image"));
e.preventDefault();
// var datastring = $("#myForm").serialize();
//alert(datastring);
var file = $('#my_image')[0].files[0];
if (file){
console.log(file);
}
$.ajax(
{
url: "${your_endpoint_url}",
type : "POST",
contentType: false,
//data: form_data.get("image"),
data: {"image.content":form_data.get("image")},
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
dataType: "json",
success: function(result){
alert('success..see response in console log');
console.log('success>>>');
console.log(result);
},
error: function(error){
alert('error..see response in console log');
console.log('error>>>');
console.log(error);
},
});
});
});})(LITHIUM.jQuery);
</@liaAddScript>

 

 

Kindly have a look and suggest any feasible way where we can upload the image with the above api.

 

Regards

Irfan