我目前正在开发一个网站,我必须在其中实施错误报告模块。在此模块中,用户将提供正在报告的错误的屏幕截图以及其他一些详细信息。我使用AJAX,jQuery将这些数据传递给控制器。但是在控制器中,Request.Files arrayList显示为空并给出异常。 我尝试了一切来解决它,但如果有人知道的话,我无法理解它。 这是HTML页面
<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 -->
这是java脚本代码
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);
});
}
};
这是控制器功能
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);
}
}
感谢
答案 0 :(得分:0)
我改变了我的方法,现在正在努力。 这是我所做的改变
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
{
var file = $("#bug_img").get(0).files;
var data = new FormData;
data.append("_file", file[0]);
data.append("_bugDes", document.getElementById("bug_des").value);
data.append("_bugUrl", document.getElementById("bug_url").value);
data.append("_bugDetail", document.getElementById("feedback").value);
$.ajax({
type: "Post",
url: "/Sponsor/sendBugReport",
data: data,
contentType: false,
processData: false,
success: function (data) {
alert(data);
}
});
}
};