图像文件未传递给控制器​​显示空值MVC

时间:2018-10-30 10:41:21

标签: c# asp.net-mvc

我想发送带有文件附件的电子邮件。当我客户端发送并使用中断检查我接收的数据时,图像文件在控制器中显示为null。有人告诉我我的错误在哪里,代码中有什么问题。所以我共享控制器代码,模型和HTML,请仔细检查我的代码并告诉我代码中有什么问题。

控制器

  [HttpPost]
        public ActionResult Index(EmployeeModel obj)
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("abc@hotmail.com", "******");
            MailMessage msgobj = new MailMessage();
            msgobj.To.Add(obj.ToEmail);
            msgobj.From = new MailAddress("abc@hotmail.com");
            msgobj.Body = obj.EMailBody;
            msgobj.Subject = obj.EmailSubject;
            msgobj.CC.Add(obj.EmailCC);
            msgobj.Bcc.Add(obj.EmailBCC);

            if(obj.imageFile !=null)
            {
                msgobj.Attachments.Add(new Attachment(obj.imageFile.InputStream.ToString(), obj.imageFile.FileName));
            }

            client.Send(msgobj);
            ViewBag.Success = "Email Send Successfully";




            return View();
        }

型号:

[DataType(DataType.EmailAddress),Display(Name = "TO")]
        [Required]
        [Key]
        public string ToEmail { get; set; }

        [DataType(DataType.MultilineText)]
        [Display(Name ="Body")]
        [Required]
        public string EMailBody { get; set; }

        [Display(Name ="Subject")]
        [Required]
        public string EmailSubject { get; set; }

        [Display(Name ="CC")]
        [DataType(DataType.EmailAddress)]
        public string EmailCC { get; set; }
        [Display(Name ="BCC")]
        [DataType(DataType.EmailAddress)]
        public string EmailBCC { get; set; }

        public HttpPostedFileWrapper imageFile { get; set; }
        public string imageUrl { get; set; }

HTML

 @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Employee Model</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ToEmail, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ToEmail, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ToEmail, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EMailBody, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMailBody, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMailBody, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailSubject, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailSubject, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailSubject, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailCC, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailCC, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailCC, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailBCC, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailBCC, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailBCC, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.imageFile, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" id="imageFile" name="imageFile" accept="image/jpeg, image/png" />

            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Send" class="btn btn-default" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10 text-success">
                @ViewBag.Status
            </div>
        </div>
    </div>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

2 个答案:

答案 0 :(得分:1)

您应该更改

 @using (Html.BeginForm()) 

使用

@using (Html.BeginForm("Index", "YourControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))

What does enctype='multipart/form-data' mean?

答案 1 :(得分:1)

首先,您应该修改此属性:

$ cat /tmp/a.json/*                                                                            
{"colA":"a","colB":{"a":"b","c":"d"}}
{"colA":"b","colB":{"foo":"bar"}}

对此:

public HttpPostedFileWrapper imageFile { get; set; }

第二,将public HttpPostedFileBase imageFile { get; set; } 添加到enctype="multipart/form-data助手中:

BeginForm

您还使用了Gmail SMTP引擎,但是您的网络凭据设置似乎使用的是Hotmail,两者的设置都不同。以下是正确设置的示例:

Gmail SMTP

@using (Html.BeginForm("Index", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   // form contents
}

Hotmail SMTP

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("xxxxx@gmail.com", "******");
MailMessage msgobj = new MailMessage();
msgobj.To.Add(obj.ToEmail);
msgobj.From = new MailAddress("xxxxx@gmail.com");
msgobj.Body = obj.EMailBody;
msgobj.Subject = obj.EmailSubject;
msgobj.CC.Add(obj.EmailCC);
msgobj.Bcc.Add(obj.EmailBCC);

if (obj.imageFile != null && obj.imageFile.ContentLength > 0)
{
    msgobj.Attachments.Add(new Attachment(obj.imageFile.InputStream, obj.imageFile.FileName));
}

client.Send(msgobj);