I want to send a message to another member of a web application.
Message will contain text and as well as attachment of images or pdf.
I have used message sending $.ajax post of JQuery with XML WebService.
Everything is okay but I cannot sent Image object with this message.
Is there anyway to save image with the message in same function?
$("#btnSend").click(function (evt) {
evt.preventDefault();
evt.stopPropagation();
if (validateMSG()) {
var mailMsg = { From: clientId, To: parseInt(msgTo), Subject: Subject, Body: body }
$.ajax({
url: "/StuffProfile.asmx/SendMessage",
data: JSON.stringify({ mailMsg: mailMsg }),
dataType: "json",
type: "POST",
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
res = $.parseJSON(result.d);
$("#staff-id").val('');
$("#txtSubject").val('');
$(".editor").val('');
$("#compose-close").click();
jAlert(res.MSG, "Success");
ajaxFileUpload();
GetNewMailMesseges(2, res.MSGID - 1);
GetMailCount(2);
},
error: function () { jAlert("Error Occured", "Error"); },
complete: function () {
}
});
}
});
function validateMSG()
{
var msgTo = $("#staff-id").val();
if (!msgTo) { jAlert("Select a Staff or Send to Please", "Error", function () { $("#txtTo").focus(); return false; }); }
else return true;
var Subject = $("#txtSubject").val();
if (Subject == '') { jAlert("Add Subject Please", "Error", function () { $("#txtSubject").focus(); return false; }) }
else return true;
var body = $(".editor").val();
if (!body) { jAlert("You cannot send empty message."); $(".editor").focus(); return false; }
else return true;
}
I want to send Image or any file with it.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = true)]
public string SendMessage( Mail mailMsg )
{
mailMsg.Read = false;
mailMsg.Status = "sent";
mailMsg.Date = DateTime.UtcNow;
try
{
var db = new WooskieDBInstance().WooskieDB;
db.Mails.InsertOnSubmit(mailMsg);
db.SubmitChanges();
var mailID = mailMsg.ID;
return new JavaScriptSerializer().Serialize(new { MSG = "Mail Send Successfully.",MSGID=mailID });
}
catch (Exception ex)
{
return ex.Message;
}
}
My HTML IS LIKE THIS
<div id="compose" class="mail ui-widget">
<img title="Close" alt="X" src="../Images/close-button.png" id="compose-close">
<div class="compose ui-widget">
<input type="text" role="textbox" class="txt-to margin-left" id="txtTo"
required="required" placeholder="Send To" style="width: 100%" />
<input type="hidden" id="staff-id" />
<br />
<input type="text" role="textbox" class="txt-subject margin-left" style="width: 100%" id="txtSubject" required="required" placeholder="Subject" />
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Message" meta:resourcekey="Label3Resource1"></asp:Label>
<textarea class="editor"></textarea>
<script src="../ckeditor/ckeditor.js"></script>
<script src="../ckeditor/adapters/jquery.js"></script>
<script type="text/javascript">
$('textarea.editor').ckeditor();
</script>
<br />
<br />
<fieldset>
<legend>Upload</legend>
<asp:FileUpload ID="fileToUpload" runat="server" ClientIDMode="Static" meta:resourcekey="attachmentResource1" Font-Size="9px" Width="100%" />
</fieldset>
</div>
</div>
Please help me.