1

I am trying to add file upload to my asp.net mvc4, however, since I am just learning C# I am not sure on how where to add it:

This is the controller:

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

the view is here:

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

ok here is the part that saves the file:

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);
            }
NULL
  • 1,559
  • 5
  • 34
  • 60

3 Answers3

0

pick up the files in the controller like so

 [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);
        }
DavidB
  • 2,566
  • 3
  • 33
  • 63
  • how about my existing declaration: "TotalReport treport" where does it go? – NULL Aug 16 '13 at 15:03
  • so i did, however, the file didn't get uploaded:( there were no errors, yet when i check the folder it was blank. forgot to this is done on localhost, so you do think that is why? localhost is my local PC, not production. – NULL Aug 16 '13 at 15:18
  • Sorry, you can add your model in as a parem as well – DavidB Aug 16 '13 at 15:44
0

just add argument for the posted file to your action :

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

and do whatever you want to do with it - read stream, save it somewhere...

rouen
  • 5,003
  • 2
  • 25
  • 48
  • 1
    ok got it! this also helped: http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0 – NULL Aug 16 '13 at 15:05
  • i was trying to do this: var fileName = Path.GetFileName(file.FileName); in my code however, it says "path does not exist in current content", how do i include it: system.path? – NULL Aug 16 '13 at 15:13
  • so i did, however, the file didn't get uploaded:( there were no errors, yet when i check the folder it was blank. forgot to this is done on localhost, so you do think that is why? localhost is my local PC, not production. – NULL Aug 16 '13 at 15:19
  • I added the part that saves the file, please see above – NULL Aug 16 '13 at 15:29
0

Suppose if your markup is like,

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

and then your action should look like,

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

here parameter name of HttpPostedFileBase and upload control name should be same. Hope this helps

Karthik Bammidi
  • 1,851
  • 9
  • 31
  • 65