Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
I am trying to upload an image using MVC 4. But it throws an exception of type 'System.NullReferenceException' occurred in GMS1.dll but was not handled in user code.

My code is as follows

HTML
<form action="" method="post" enctype="multipart/form-data">

  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />

  <input type="submit" />
</form>


C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GMS1.Controllers
{
    public class FileUploadController : Controller
    {
        //
        // GET: /FileUpload/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {

            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/UserImages/"), fileName);
                file.SaveAs(path);
            }

            return RedirectToAction("Index");
        }

    }
}


can anyone would like to solve this issue ? Any help would be grateful .. Thanks in advance
Posted
Comments
Sinisa Hajnal 15-Sep-14 9:29am    
Start with handling the error. Add Try Catch block around your function and see what is the problem. My guess would be access rights.
[no name] 18-Sep-14 2:04am    
Thank You.

1 solution

These links may helpful
http://stackoverflow.com/questions/16255882/how-to-upload-image-display-image-in-asp-net-mvc-4[^]

http://stackoverflow.com/questions/20446580/upload-image-included-in-mvc-model[^]


HTML
@using (Html.BeginForm("Index", "FileUploader", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   <label for="file">Filename:</label>
  <input type="file" name ="file" id="file" />
  <input type="submit" />
}
 
Share this answer
 
v2
Comments
[no name] 23-Sep-14 6:32am    
i am Grateful for this... Thanks a lot..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900