gpt4 book ai didi

asp.net-mvc - 如何在 MVC + Umbraco 7 中上传文件

转载 作者:行者123 更新时间:2023-12-01 23:45:37 24 4
gpt4 key购买 nike

我是 MVC 的新手,需要帮助上传 File ,我正在使用 umbraco。 7.2.1
我正在尝试使用带附件的邮件发送邮件
以下是我的代码。
部分查看==>姓名联系方式

 using (Html.BeginUmbracoForm<ContactVController>("HandleContactSubmit"))   
{
@Html.LabelFor(model => model.Name)<br />
@Html.EditorFor(model => model.Name)<br />
@Html.ValidationMessageFor(model => model.Name)<br />

@Html.LabelFor(model => model.Email)<br />
@Html.EditorFor(model => model.Email)<br />
@Html.ValidationMessageFor(model => model.Email)<br />
<input type="file" name="file" id="file" />
<p>
<input type="submit" value="Submit" />
</p>
}

型号
public class ContactVModel  
{
[Required]
public string Name { get; set; }
[Required]
[RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]
public string Email { get; set; }
[Required]
public string Message { get; set; }
public HttpPostedFileBase attachment { get; set; }
}

Controller
public class ContactVController : SurfaceController
{
[HttpPost]
public ActionResult HandleContactSubmit(ContactVModel model)
{
,.......... ...... ....

,.......... ...... ....

MailBody + = model.Name ;
MailBody + = model.Email;

SendMail( MailBody )
}

但我不知道访问 model.attachment ,我该如何发送带有附件的邮件(上传的文件)(因为我可以访问姓名、电子邮件等)
我引用了以下帖子,但我无法访问附件

MVC 4 Razor File Upload

但我无法理解

最佳答案

更改输入

<input type="file" name="file" id="file" />


<input type="file" name="attachment" id="attachment" /> 

因此模型中的属性与输入字段匹配。

我目前正在使用以下内容:
In the cshtml file:
@Html.UploadFor(model=> model.Attachment)

static public class HtmlExtensions
{
public static IHtmlString UploadFor<TSource, TResult>(this HtmlHelper<TSource> html, Expression<Func<TSource, TResult>> propertyExpression)
{
var memberName = Reflection.GetPropertyName(propertyExpression);
return new HtmlString($"<input type=\"file\" name=\"{memberName}\" id=\"{memberName}\">");
}
}

class Reflection
{
public static string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
}

关于asp.net-mvc - 如何在 MVC + Umbraco 7 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29353244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com