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,37 +654,14 @@ 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
if(form.inputImage.files[0]){
//Include the image with the request (export the image as URL)
var reader = new FileReader();
reader.readAsDataURL(form.inputImage.files[0]);
var sendImage = reader.result;
//The function will resume once the image is fully converted
reader.addEventListener("load", function() {
//Send the message throught the interface //Send the message throught the interface
ComunicWeb.components.conversations.interface.sendMessage({ ComunicWeb.components.conversations.interface.sendMessage({
conversationID: convInfos.infos.ID, conversationID: convInfos.infos.ID,
message: form.inputText.value, message: form.inputText.value,
image: reader.result, image: form.inputImage,
callback: onceSent 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 hasImage = false;
if(infos.image){
if(infos.image.files[0])
hasImage = true;
}
//Check wether an image has to be included or not
if(!hasImage){
//Prepare request
var params = { var params = {
message: infos.message, message: infos.message,
conversationID: infos.conversationID, conversationID: infos.conversationID,
} };
//Add an image (if any specified)
if(infos.image)
params.image = infos.image;
//Perform an API request //Perform an API request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback); 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);
}
}, },
/** /**