asp.net来自sql并上传到app_data

时间:2015-10-29 10:35:19

标签: asp.net-mvc forms file

我想为求职应用程序创建一个表单,将个人详细信息保存在数据库中,将文件(cv)保存到app_data,我可以将个人详细信息保存到sql db但我无法将文件保存到app数据,帮助meeeeeeeeeee 的模型

[Required(ErrorMessage = "Required")]
public string Name { get; set; }

[Required(ErrorMessage = "Required")]
[RegularExpression(@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$", ErrorMessage = "Must be valid email")]
public string Email { get; set; }

[Required(ErrorMessage = "Required")]
public string Comments { get; set; }


[Required(ErrorMessage = "Required")]
public HttpPostedFileBase file { get; set; }

控制器

public ActionResult SaveDataContact(ContactModel f)
        {
            if (ModelState.IsValid)
            {
                D.Open();
                if (f.file != null && f.file.ContentLength > 0)
                {
                    // extract only the filename
                    var fileName = Path.GetFileName(f.file.FileName);
                    // store the file inside ~/App_Data/uploads folder
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    f.file.SaveAs(path);
                }

                int i = D.DataInsert("INSERT INTO table_cont(Name,Email,Comments) VALUES ('" + f.Name + "','" + f.Email + "','" + f.Comments + "')");

chtml表单视图

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


 @Html.ValidationSummary();
 @Html.AntiForgeryToken();


@Html.LabelFor(model => model.Name)<br/>
@Html.TextBoxFor(model => model.Name)<br />


@Html.LabelFor(model => model.Email)<br />
@Html.TextBoxFor(model => model.Email)<br />

@Html.LabelFor(model => model.Comments)<br />
@Html.TextAreaFor(model => model.Comments)<br />


<label for="file">Upload your Cv:</label>
<input type="file" name="file" /><br />

1 个答案:

答案 0 :(得分:0)

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


@Html.ValidationSummary();
@Html.AntiForgeryToken();


@Html.LabelFor(model => model.Name)<br/>
@Html.TextBoxFor(model => model.Name)<br />


@Html.LabelFor(model => model.Email)<br />
@Html.TextBoxFor(model => model.Email)<br />

@Html.LabelFor(model => model.Comments)<br />
@Html.TextAreaFor(model => model.Comments)<br />


<label for="file">Upload your Cv:</label>
@Html.TextBoxFor(m => m.file, new { type = "file", name = "file" })<br />