Im trying to convert a fileupload form data to base64. An ajax post sends the form data to backend.
$( "#profileModalForm" ).submit(function( event ) {
var formData = new FormData(this);
$.ajax({
cache: false,
url: 'SaveProfilePopupData.ws',
type: "POST",
enctype: 'multipart/form-data',
data: formData,
contentType: false,
processData: false,
success: function (html) {
$('#notificationArea').html(html);
}
});
event.preventDefault();
});
Then in java side im trying to read the image using apache commons fileupload and save to base64 string.
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while(iterator.hasNext()){
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if(!item.isFormField()){
byte[] str = new byte[stream.available()];
stream.read(str);
imageBase64String = new String(Base64.getEncoder().encode(str));
}
}
I am only able to get a partial base64 string value of the image i am uploading(In a 7KB image, i can only see a half of the image). What am I doing wrong?