I am currently working on a website in which i have to implement a bug reporting module. In this module user will provide the screen-shot of the bug being reported with some other details. I am passing this data to controller using AJAX, jQuery. But in controller the Request.Files arrayList is shown empty and gives exception. I tried everything to resolve it but can't figured it out kindly help if anyone know. Here is the HTML page
<div class="row mt">
<div class="col-lg-12">
<div class="form-panel">
<h4 class="mb"><i class="fa fa-angle-right"></i> Please provide Details about bug you have found</h4>
<form class="form-horizontal style-form" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Brief Description</label>
<div class="col-sm-10">
<input type="text" class="form-control round-form" placeholder="Provide brief description about bug here" id="bug_des">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">URL</label>
<div class="col-sm-10">
<input type="url" placeholder="Please provide ulr of the page where you found this bug" id="bug_url" class="form-control round-form">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Screen-Shot</label>
<div class="col-sm-10">
<input class="form-control round-form" type="file" placeholder="Please provide screenshot of the bug" id="bug_img" accept="image/*">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Aditional Details</label>
<div class="col-sm-10">
<textarea class="form-control round-form" placeholder="Provide additionl details here...." id="feedback" tabindex="5" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<button type="button" class="btn btn-primary btn-lg btn-block" onclick="getBugReport()">Submit</button>
</div>
</div>
</form>
</div>
</div><!-- col-lg-12-->
</div><!-- /row -->
This is the java Script code
function getBugReport() {
var content = document.getElementById("bug_url").value;
if (content.length < 1) {
alert("url field cant be left empty");
return false;
}
content = document.getElementById("bug_des").value;
if (content.length < 1) {
alert("Description field cant be left empty");
return false;
}
if (document.getElementById("bug_img").value == "") {
alert("please provide a screenshot of bug");
return false;
}
content = document.getElementById("feedback").value;
if (content.length < 1) {
alert("Message field cant be left empty");
return false;
}
else {
$.getJSON("/Sponsor/sendBugReport/?_bugDes=" + document.getElementById("bug_des").value + "&_bugUrl=" + document.getElementById("bug_url").value + "&_bugDetail=" + document.getElementById("feedback").value, function (data) {
document.getElementById("feedback").value = "";
document.getElementById("bug_des").value = "";
document.getElementById("bug_url").value = "";
alert(data);
});
}
};
Here is the controller function
public JsonResult sendBugReport(String _bugDes, Uri _bugUrl, String _bugDetail)
{
try
{
if ((_bugDes != null) && (_bugUrl != null) && (_bugDetail != null) && (Request.Files[0] != null))
{
HttpPostedFileBase file = Request.Files[0];
string img = @"~\Files\" + file.FileName;
file.SaveAs(Server.MapPath(img));
string from = "muhammadusama387@gmail.com";
using (MailMessage mail = new MailMessage(from, "ishtiyar.customer.service@gmail.com"))
{
mail.Subject = _bugDes;
mail.Body = "URL:\n" + _bugUrl + "\nDetails:\n" + _bugDetail;
mail.Attachments.Add(new Attachment(img));
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(from, "");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
return this.Json("Your Bug Report has been Submitted Successfully!!! Thank You", JsonRequestBehavior.AllowGet);
}
}
else
{
return this.Json("Something went wrong!.... Please provide complete details", JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
return this.Json(ex.ToString(), JsonRequestBehavior.AllowGet);
}
}
thanks