我的网站上有反馈表单,我需要在电子邮件中添加附件 我有
@Html.TextBoxFor(model => model.ProjectInformation, null, new { type = "file", @class = "input-file" })
=
<input type="file" id="ProjectInformation">
我在控制器中写了那段代码
//Attachment
if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
{
HttpPostedFileBase hpf = this.Request.Files[Model.ProjectInformation.FileName];
if (hpf.ContentLength > 0)
{
var attach = new Attachment(hpf.InputStream, Model.ProjectInformation.FileName);
msg.Attachments.Add(attach);
}
}
但是我收到了没有附件的电子邮件,当我检查调试器时,我看到了
Model.ProjectInformation = null
但我不明白为什么?
答案 0 :(得分:1)
Model.ProjectInformation
已经是HttpPostedFileBase
来自您的观点,您无需尝试从请求中获取文件。
[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
//Attachment
if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
{
HttpPostedFileBase hpf = Model.ProjectInformation;
if (hpf.ContentLength > 0)
{
var attach = new Attachment(hpf.InputStream, Model.ProjectInformation.FileName);
msg.Attachments.Add(attach);
}
}
}