Conversation system send images through FormData

This commit is contained in:
Pierre 2018-04-21 11:00:13 +02:00
parent 348cf881d5
commit 4e295b9a16
2 changed files with 36 additions and 40 deletions

View File

@ -654,36 +654,13 @@ ComunicWeb.components.conversations.chatWindows = {
ComunicWeb.components.conversations.chatWindows.resetCreateMessageForm(convInfos); ComunicWeb.components.conversations.chatWindows.resetCreateMessageForm(convInfos);
} }
//Check if an image is included with the message or not //Send the message throught the interface
if(form.inputImage.files[0]){ ComunicWeb.components.conversations.interface.sendMessage({
//Include the image with the request (export the image as URL) conversationID: convInfos.infos.ID,
var reader = new FileReader(); message: form.inputText.value,
reader.readAsDataURL(form.inputImage.files[0]); image: form.inputImage,
var sendImage = reader.result; callback: onceSent
});
//The function will resume once the image is fully converted
reader.addEventListener("load", function() {
//Send the message throught the interface
ComunicWeb.components.conversations.interface.sendMessage({
conversationID: convInfos.infos.ID,
message: form.inputText.value,
image: reader.result,
callback: onceSent
});
}, false);
}
else {
//Send the message throught the interface
ComunicWeb.components.conversations.interface.sendMessage({
conversationID: convInfos.infos.ID,
message: form.inputText.value,
callback: onceSent
});
}
//Success //Success
return true; return true;

View File

@ -242,7 +242,7 @@ ComunicWeb.components.conversations.interface = {
* @param {Object} infos Informations about the message to send * @param {Object} infos Informations about the message to send
* @info {Integer} conversationID The ID of the conversation * @info {Integer} conversationID The ID of the conversation
* @info {String} message The message to send * @info {String} message The message to send
* @info {String} image Optionnal, base64-encoded image * @info {HTMLElement} image Optionnal, input field with an image
* @info {function} callback What to do once the image was successfully sent * @info {function} callback What to do once the image was successfully sent
* @return {Boolean} true for a success * @return {Boolean} true for a success
*/ */
@ -250,18 +250,37 @@ ComunicWeb.components.conversations.interface = {
//Perform an API request //Perform an API request
var apiURI = "conversations/sendMessage"; var apiURI = "conversations/sendMessage";
var params = {
message: infos.message, var hasImage = false;
conversationID: infos.conversationID, if(infos.image){
if(infos.image.files[0])
hasImage = true;
} }
//Add an image (if any specified) //Check wether an image has to be included or not
if(infos.image) if(!hasImage){
params.image = infos.image;
//Perform an API request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback);
//Prepare request
var params = {
message: infos.message,
conversationID: infos.conversationID,
};
//Perform an API request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback);
}
//If we have an image, we must do a formdata request
else {
var fd = new FormData();
fd.append("message", infos.message);
fd.append("conversationID", infos.conversationID);
fd.append("image", infos.image.files[0], infos.image.files[0].name);
//Perform an API request
ComunicWeb.common.api.makeFormDatarequest(apiURI, fd, true, infos.callback);
}
}, },
/** /**