Fórum

How to pass base64encode string through XMLHttpRequest() post request

thumbnail
Balakrishna Donthula, modificado 8 Anos atrás.

How to pass base64encode string through XMLHttpRequest() post request

New Member Postagens: 3 Data de Entrada: 24/10/14 Postagens Recentes
I am trying to upload a image to server,In that i have converted image to base64encode string and i need to pass that base64encode string to webservice,that webservice convert the base64 string to file and saving in database.but base64encode string has huge length approximately(85,000) when i pass this string to webservice i am getting the following error.

Failed to load resource: the server responded with a status of 400 (Bad Request)
i need to pass this by using only XMLHttpRequest() with out using the ajax,jquery please help me.

below is my code.

var filesToBeUploaded = document.getElementById("afile");
var file = filesToBeUploaded.files[0];
var reader = new FileReader();
reader.onload = function(event) {
var binaryStringResult = reader.result;

var binaryString =binaryStringResult.split(',')[1];
var xhr = new XMLHttpRequest();
xhr.open("POST","http://url/api/jsonws/Global-portlet.org_roles/add-file-entry?repositoryId=11304&folderId=0&sourceFileName=test108.jpeg&mimeType=image%2Fjpeg&title=test108.jpeg&description=test108.jpeg&changeLog=test108.jpeg&basecode64="+ binaryString);


xhr.setRequestHeader("Authorization","BasicbmFyYXlhbmFAdmlkeWF5dWcuY29tOnRlc3Q=");
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');

xhr.send();

xhr.onload = function() {
alert('in sucess');

};
xhr.onerror = function(e) {
alert('in error');
};

}
reader.readAsDataURL(file);

i have also tried with different requestsetHeaders like

xhr.setRequestHeader("Content-Type", "multipart/form-data; charset=utf-8;');
xhr.setRequestHeader("FileType", "image/jpg");
thumbnail
David H Nebinger, modificado 8 Anos atrás.

RE: How to pass base64encode string through XMLHttpRequest() post request

Liferay Legend Postagens: 14917 Data de Entrada: 02/09/06 Postagens Recentes
You're not going to be able to pass this as a url parameter, you're going to have to pass it within the body of the POST.