2013-08-16 60 views
1

我想上传文件添加到我的asp.net mvc4,但是,因为我刚学C#我不知道如何在何处添加它:文件上传与MVC应用程序

这是控制器:

public ActionResult Create() 
     { 
      ViewBag.c_id = new SelectList(db.Cities.OrderBy(o => o.name), "c_id", "name"); 
      ViewBag.m_id = new SelectList(db.Schools, "m_id", "name"); 

      return View(); 
     } 

     // 
     // POST: /Create 

     [HttpPost] 
     public ActionResult Create(TotalReport treport) 
     { 
      if (ModelState.IsValid) 
      { 
       treport.created = DateTime.Now; 

       db.TotalReports.Add(treport); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.c_id = new SelectList(db.Cities.OrderBy(o => o.name), "c_id", "name"); 
      ViewBag.m_id = new SelectList(db.Schools, "m_id", "name"); 

      return View(treport); 
     } 

的观点是在这里:

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

    <fieldset> 
<div class="mycss"> 
     <input type="file" name="file" /> 
    </div> 
</fieldset> 

确定这里是保存该文件的一部分:

if (file != null && file.ContentLength > 0) 
      { 
       // extract only the fielname 
       var fileName = System.IO.Path.GetFileName(file.FileName); 
       // store the file inside ~/App_Data/uploads folder 
       var path = System.IO.Path.Combine(Server.MapPath("~/myfolder"), fileName); 
       file.SaveAs(path); 
      } 

回答

0

假设您的标记是否喜欢,

<input type="file" name="file" /> 

,然后你的行动应该是什么样子,

[HttpPost] 
    public ActionResult(HttpPostedFileBase file) 
    { 
    string filename=file.FileName; 
    filename=DateTime.Now.ToString("YYYY_MM_dddd_hh_mm_ss")+filename; 
    file.SaveAs("your path"+filename); 
return View(); 
    } 

这里HttpPostedFileBase的参数名称和上传控件的名称应该是相同的。希望这可以帮助

+0

我能够上传文件和所有,但是,它不会被保存! – NULL

+0

您是否将文件数据导入文件参数?并检查路径是否存在。 –

+0

是的文件进入参数,和所有。然而,当我检查文件夹时,它不在那里。我在Windows 7上。 – NULL

0

拿起文件在控制器像这样

[HttpPost] 
     public ActionResult Create(HttpPostedFileBase fileUpload) 
     { 
      if (ModelState.IsValid) 
      { 
       treport.created = DateTime.Now; 

       db.TotalReports.Add(treport); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.c_id = new SelectList(db.Cities.OrderBy(o => o.name), "c_id", "name"); 
      ViewBag.m_id = new SelectList(db.Schools, "m_id", "name"); 

      return View(treport); 
     } 
+0

我现有的声明如何:“TotalReport treport”它到底在哪里? – NULL

+0

所以我做了,但是,文件没有上传:(没有错误,但当我检查文件夹是空白的。忘记这是在localhost上完成的,所以你认为这是为什么?localhost是我的本地PC,不生产 – NULL

+0

对不起,你可以添加你的模型作为parem以及 – DavidB

0

只是为了发布文件添加参数到你的动作:

public ActionResult Create(TotalReport treport, System.Web.HttpPostedFileBase file) 

,做任何你想用它做的 - 读流,保存到某个地方......

+1

确定它!这也帮助:http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc -3-0 – NULL

+0

我试图做到这一点:var fileName = Path.GetFileName(file.FileName);但在我的代码中,它说“路径不存在于当前内容中”,我如何包含它:system.path ? – NULL

+0

所以我做了,但是,文件没有得到上传:(有没有错误,但当我检查文件夹它是空白。忘记这是在localhost上完成的,所以你认为这是为什么?localhost是我的本地PC不生产 – NULL