我一直在尝试在我正在制作的网站上附加/上传多个文件。姓名,电子邮件,主题和&消息正在发送,但消息中没有附件。似乎文件没有进入uploads文件夹。我真的不知道什么是错的。请帮我。我对这种东西很新。谢谢。以下是我的观点:
@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="col-md-4">
<div class="contact_form block">
<div class="row">
<div class="col-md-12 col-sm-12">
<div id="note"></div>
</div>
</div>
<div id="fields">
<div class="col-md-12 col-sm-6">
@Html.LabelFor(m => m.FromName)
@Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromName)
</div>
<div class="col-md-12 col-sm-6">
@Html.LabelFor(m => m.FromEmail)
@Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromEmail)
</div>
<div class="clear"></div>
<div class="col-md-12 col-sm-6">
@Html.LabelFor(m => m.FromSubject)
@Html.TextBoxFor(m => m.FromSubject, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromSubject)
</div>
<div class="col-md-12">
@using (Html.BeginForm("Multiple", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div id="multiple">
<input type="file" class="multiple" name="files" multiple />
</div>
<div id="single">
<input type="file" class="single" name="files" /><br />
<input type="file" class="single" name="files" /><br />
<input type="file" class="single" name="files" /><br />
</div>
}
</div>
<div class="col-md-12">
@Html.LabelFor(m => m.Message)
@Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Message)
</div>
<div class="col-md-12">
<div>
@if ((TempData["recaptcha"]) != null)
{
<p>@TempData["recaptcha"]</p>
}
</div>
<div class="g-recaptcha" data-sitekey="6LfVHx8TAAAAAMTDxxQrHDCxO1SyXf1GgbgNBZ5a"></div>
</div>
<div class="col-md-12"><input class="shortcode_button" type="submit" value="Send"></div>
</div>
</div>
</div>
}
这是我的控制者:
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(EmailFormModel model)
{
if (ModelState.IsValid)
{
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
if(IsCaptchaValid)
{
var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
var message = new MailMessage();
message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
message.From = new MailAddress("***@ymailcom"); // replace with valid value
message.Subject = "Your email subject";
message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "***@gmail.com", // replace with valid value
Password = "***" // replace with valid value
};
smtp.Credentials = credential;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message);
//return RedirectToAction("Sent");
ViewBag.Message = "Your message has been sent!";
//TempData["message"] = "Message sent";
ModelState.Clear();
return View("Index");
}
}else
{
TempData["recaptcha"] = "Please verify that you are not a robot!";
}
}
return View(model);
}
[HttpPost]
public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
file.SaveAs(Path.Combine(Server.MapPath("/uploads"), Guid.NewGuid() + Path.GetExtension(file.FileName)));
}
}
return View();
}
答案 0 :(得分:0)
您的第一行代码就是问题。
Html.BeginForm("Index", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data"
这是对家庭控制器的索引的Post动作。 HttpPost是不可能的另一个HttpPost,动作本身就是期待一个HttpPost,正如你可以通过ActionResult名称上面的数据注释看到的那样
为什么使用嵌套表格?
答案 1 :(得分:0)
您的视图不应包含嵌套表单提交。所以将它减少到一个,同样可以用来上传文件。
@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="col-md-4">
<div class="contact_form block">
<div class="row">
<div class="col-md-12 col-sm-12">
<div id="note"></div>
</div>
</div>
<div id="fields">
<div class="col-md-12 col-sm-6">
@Html.LabelFor(m => m.FromName)
@Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromName)
</div>
<div class="col-md-12 col-sm-6">
@Html.LabelFor(m => m.FromEmail)
@Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromEmail)
</div>
<div class="clear"></div>
<div class="col-md-12 col-sm-6">
@Html.LabelFor(m => m.FromSubject)
@Html.TextBoxFor(m => m.FromSubject, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromSubject)
</div>
<div class="col-md-12">
{
<div id="multiple">
<input type="file" class="multiple" name="files" multiple />
</div>
<div id="single">
<input type="file" class="single" name="files" /><br />
<input type="file" class="single" name="files" /><br />
<input type="file" class="single" name="files" /><br />
</div>
</div>
<div class="col-md-12">
@Html.LabelFor(m => m.Message)
@Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Message)
</div>
<div class="col-md-12">
<div>
@if ((TempData["recaptcha"]) != null)
{
<p>@TempData["recaptcha"]</p>
}
</div>
<div class="g-recaptcha" data-sitekey="6LfVHx8TAAAAAMTDxxQrHDCxO1SyXf1GgbgNBZ5a"></div>
</div>
<div class="col-md-12"><input class="shortcode_button" type="submit" value="Send"></div>
</div>
</div>
</div>
}
Moreever为Action添加一个参数,以便从客户端请求接收文件并以相同的操作处理它。
public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
//logic here upload file logic here.
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
file.SaveAs(Path.Combine(Server.MapPath("/uploads"), Guid.NewGuid() + Path.GetExtension(file.FileName)));
}
}
//Rest of business logic here
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
if(IsCaptchaValid)
{
var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
var message = new MailMessage();
message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
message.From = new MailAddress("***@ymailcom"); // replace with valid value
message.Subject = "Your email subject";
message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "***@gmail.com", // replace with valid value
Password = "***" // replace with valid value
};
smtp.Credentials = credential;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message);
//return RedirectToAction("Sent");
ViewBag.Message = "Your message has been sent!";
//TempData["message"] = "Message sent";
ModelState.Clear();
return View("Index");
}
}else
{
TempData["recaptcha"] = "Please verify that you are not a robot!";
}
}
return View(model);
}