jeudi 29 octobre 2015

asp.net from with sql and upload to app_data

i want to make a form for job application which save the personal details in database and a file(cv) to app_data, i can save the personal details to sql db but i can not save the file to app data,help meeeeeeeeeee model

[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; }

controller

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 form view

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

Aucun commentaire:

Enregistrer un commentaire