将文件上传到asp.net mvc中的根文件夹的问题

时间:2014-06-23 06:44:44

标签: c# asp.net asp.net-mvc

我是这个asp.net mvc的新手,因为我之前曾经使用过webforms。我手边有一个项目需要将文件上传到根服务器文件夹。

我的解决方案中有一个名为“Photos”的根文件夹,每当我保存文件时,它只会将文件夹保留为空,但我可以将文件的文件名保存到我的数据库中。

我使用了html文件上传控件**<input type="file" id="Photos" name="Photos" title="Upload a Photo" />**。如何将实际文件保存到名为“Photos”的根文件夹中?

为了让你进一步了解,我会给你我的代码。以下是......谢谢

[HttpPost]
public ActionResult Create(ProfileModel user)
{
    // var fileimage = Request.Files[0];
    HttpFileCollectionBase hpf = Request.Files;
    HttpPostedFileBase file = hpf[0];
    string fname = System.IO.Path.GetFileName(file.FileName.ToString());

    if (file != null && file.ContentLength > 0)
    {
        file.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Photos/") + System.IO.Path.GetFileName(file.FileName.ToString())));
    }

    if (ModelState.IsValid)
    {

        var addnewrec = new ProfTab()
        {
            ID = user.ID,
            Name = user.Name,
            Address = user.Address,
            Age = Convert.ToInt32(user.Age),
            Occupation = user.Occupation,
            Nationality = user.Nationality,
            // Photos=item.Photos 
            Photos = fname
        };
        repository.ProfTabs.Add(addnewrec);
        repository.SaveChanges();
        //   _repository.InsertRecord(user);                    
        return RedirectToAction("Index", "Home");

    }

    return View(user);
}

创建视图

 @model MyFirstMVCApp.Models.ProfileModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm("Create", "Profile", FormMethod.Post, new { enctype="multipart/form-data"}))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ProfileModel</legend>
        <div style="float:left; margin-right:20px;width:49%">
         <div class="editor-label">
            @Html.LabelFor(model => model.ID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ID)
            @Html.ValidationMessageFor(model => model.ID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>
  </div>

     <div style="float:right;width:48%">
        <div class="editor-label">
            @Html.LabelFor(model => model.Age)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Occupation)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Occupation)
            @Html.ValidationMessageFor(model => model.Occupation)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Nationality)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nationality)
            @Html.ValidationMessageFor(model => model.Nationality)
        </div>
         <label for="uploadphoto">Select a file:</label>
         <input type="file" id="Photos" name="Photos" title="Upload a Photo" />

  </div>
        <p>
            <input type="submit" value="Create"  />
             @Html.ActionLink("Back to List", "Index")
        </p>
    </fieldset>
}

0 个答案:

没有答案