首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Postal的附件不起作用

带有Postal的附件不起作用
EN

Stack Overflow用户
提问于 2011-09-17 19:14:04
回答 1查看 1.9K关注 0票数 1

我有一个MVC3 project。在我的联系页面,我想增加一个可能性,增加一个附件的电子邮件发送的客户。

我正在使用Postal。它工作得很好,但我不能让附件起作用。

下面是我的代码中处理以下内容的部分:

我有一个model class

代码语言:javascript
复制
public class ContactEmail
{
    [ScaffoldColumn(false)]
    public int id { get; set; }

    [Required(ErrorMessage = "Name is required!")]
    [DataType(DataType.Text)]
    [DisplayName("Name")]
    public string name { get; set; }

    [Required(ErrorMessage = "Email Address is required!")]
    [DataType(DataType.EmailAddress)]
    [DisplayName("Email Address")]
    [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email Address is not valid.")]
    public string email { get; set; }

    [Required(ErrorMessage = "Country is required!")]
    [DisplayName("Country")]
    public string country { get; set; }

    [DisplayName("Subject")]
    public string subject { get; set; }

    [Required(ErrorMessage = "Message is required!")]
    [DisplayName("Message")]
    [StringLength(1500)]
    public string message { get; set; }

    [ScaffoldColumn(false)]
    public DateTime datetime { get; set; }

    public HttpPostedFileBase attachment { get; set; }

这是我的Postal Controller

代码语言:javascript
复制
public class PostalController : Controller
{
    [HttpPost]
    public ActionResult Send(ContactEmail model)
    {

        if (ModelState.IsValid)
        {
            Random random = new Random();
            var ticket = random.Next(1000000000, 2000000000);

            dynamic courriel = new Email("Postal");
            courriel.To = "support@coderfortraders.com";
            courriel.Name = model.name;
            courriel.Country = model.country;
            courriel.Subject = model.subject;
            courriel.From = model.email;
            courriel.Message = model.message;
            courriel.TicketId = ticket;
            courriel.Attachment = model.attachment;
            //if (model.Attachment != null && model.Attachment.ContentLength > 0)
            //{
            //    var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
            //    courriel.Attachments.Add(attachment);
            //}
            //courriel.Attachment = model.Attachment; 

            courriel.Attach(new Attachment(model.attachment.InputStream, model.attachment.FileName));

            Task task = courriel.SendAsync();

            //courriel.Send();

            return RedirectToAction("Sent", "Contact");
        }

        ViewData["MessageSent"] = false;

        return View("~/Views/Contact/Contact.cshtml");
    }

最后,这里是我在Contact.cshtml view中使用的表单

代码语言:javascript
复制
@using (Html.BeginForm("Send", "Postal"))
{        
    @Html.ValidationSummary(false)

    <p>
       @Html.LabelFor(model => model.name)
       @Html.TextBoxFor(model => model.name, new { @size = 35 })
       @Html.ValidationMessageFor(model => model.name, "*")
    </p>                      
    <p>
       @Html.LabelFor(model => model.email)
       @Html.TextBoxFor(model => model.email, new { @size = 35 })
       @Html.ValidationMessageFor(model => model.email, "*")
    </p>
    <p>
       @Html.LabelFor(model => model.subject)
       @Html.DropDownList(
           "subject", 
           new[] { 
               new SelectListItem { 
                   Text = "NT Indicator Quote", 
                   Value = "NT Indicator Quote" 
               }, 
               new SelectListItem { 
                   Text = "NT Strategy Quote", 
                   Value = "NT Strategy Quote" 
               }, 
               new SelectListItem { 
                   Text = "Comments", 
                   Value = "Comments" 
               }, 
               new SelectListItem { 
                   Text = "other", 
                   Value = "other" 
               }
           }
       ); // @Html.DropDownList(
       @Html.ValidationMessageFor(model => model.subject, "*")
    </p>
    <p>
       @Html.LabelFor(model => model.message)
       @Html.TextAreaFor(model => model.message, new { @cols = 50, @rows = 10 })
       @Html.ValidationMessageFor(model => model.message, "*")
    </p>
    <p>
       @Html.LabelFor(model => model.attachment)
       @Html.TextBoxFor(model => model.attachment, new { type = "file" })
       @Html.ValidationMessageFor(model => model.attachment)
    </p>          
    <p><input type="submit" id="submit" name="submit" value="Submit" /></p>            
} <!-- End form -->

我收到以下错误消息:

对象引用未设置为对象的实例。描述:在执行当前web请求时发生了未处理的异常。请查看堆栈跟踪以获得有关错误的更多信息,以及它起源于代码的位置。 异常详细信息: System.NullReferenceException:对象引用未设置为对象的实例。 源错误:第44行:courriel.Attach(新附件(model.attachment.InputStream,model.attachment.FileName));

你能帮我找出这里的问题吗?谢谢!

以下是视图源中的HTML:

代码语言:javascript
复制
<form action="/Postal/Send?enctype=multipart%2Fform-data" method="post">
    <div class="validation-summary-valid" data-valmsg-summary="true">
        <ul>
            <li style="display:none"></li>
        </ul>
    </div>
    <p>
        <label for="name">Name</label>
        <input data-val="true" data-val-required="Name is required!" id="name" name="name" size="35" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="name" data-valmsg-replace="false">*</span>
    </p>
    <p>
        <label for="email">Email Address</label>
        <input data-val="true" data-val-regex="Email Address is not valid." data-val-regex-pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" data-val-required="Email Address is required!" id="email" name="email" size="35" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="false">*</span>
    </p>
    <p>
        <label for="subject">Subject</label>
        <select id="subject" name="subject">
            <option value="NT Indicator Quote">NT Indicator Quote</option>
            <option value="NT Strategy Quote">NT Strategy Quote</option>
            <option value="Comments">Comments</option>
            <option value="other">other</option>
        </select>
        <span class="field-validation-valid" data-valmsg-for="subject" data-valmsg-replace="false">*</span>
    </p>
    <p>
        <label for="message">Message</label>
        <textarea cols="50" data-val="true" data-val-length="The field Message must be a string with a maximum length of 1500." data-val-length-max="1500" data-val-required="Message is required!" id="message" name="message" rows="10">
        </textarea>
        <span class="field-validation-valid" data-valmsg-for="message" data-valmsg-replace="false">*</span>
    </p>
    <p>
        <label for="attachment">attachment</label>
        <input id="attachment" name="attachment" type="file" value="" />
        <span class="field-validation-valid" data-valmsg-for="attachment" data-valmsg-replace="true"></span>
    </p>
    <p>
        <input type="submit" id="submit" name="submit" value="Submit" />
    </p>
</form> <!-- End form -->

    </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-17 19:17:26

如果要上载文件,则需要将enctype="multipart/form-data"属性附加到表单:

代码语言:javascript
复制
@using (Html.BeginForm("Send", "Postal", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    ...
}

还可以通过哈克得来查看这个博客帖子

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7457176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档