我对 MVC 比较陌生,而且我从来不需要处理将文件(特别是图像)上传到 SQL Server 数据库的问题。老实说,我不知道我在这里做什么。
这是我到目前为止所拥有的 - 这是我的域模型(注意HttpPostedFileBase
我的模型中的 - 这是我要上传的):
public class Profile
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="Years of service is required")]
[DisplayName("Years Service:")]
public int YearsService { get; set; }
[DataType(DataType.MultilineText)]
[DisplayName("Notable Achivements:")]
public string NotableAchivements { get; set; }
[Required(ErrorMessage = "Technical skills are required")]
[DataType(DataType.MultilineText)]
[DisplayName("Technical Skills:")]
public string TechnicalSkills { get; set; }
[DisplayName("Upload Image: ")]
public HttpPostedFileBase Photo { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
}
这是我的观点:
@using (Html.BeginForm("Create", "Profiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
@Html.LabelFor(model => model.YearsService)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.YearsService)
@Html.ValidationMessageFor(model => model.YearsService)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NotableAchivements)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NotableAchivements)
@Html.ValidationMessageFor(model => model.NotableAchivements)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TechnicalSkills)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TechnicalSkills)
@Html.ValidationMessageFor(model => model.TechnicalSkills)
</div>
<input type="file" name="photo" />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
我希望有一些明显的事情表明我做错了。谁能提供有关如何将简单的文件上传到 SQL Server 数据库的建议?