2015-10-29 15 views
0

我想做一個工作應用程序的表格,保存在數據庫中的個人信息和文件(cv)到app_data,我可以將個人信息保存到SQL數據庫,但我可以不將文件保存到應用程序的數據,幫助meeeeeeeeeee 模式asp.net與sql和上傳到app_data

[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]+)*)[email protected][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 /> 

+0

你得到任何錯誤? –

+0

而且,你真的需要將文件保存在App_Data中?您可以創建一個文件夾* data *並將文件保存在那裏。 – adricadar

+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 />