Search code examples
asp.net-mvcasp.net-mvc-3tempdata

Inconvenient to upload files to the server and use TempData


I'm trying to make a file upload to the server using Uploadify, but not working the TempData to pass variables between controllers and I have not found an error.

I'm trying passing the variables fileName and file with TempData of the controller "GetFile" to the controller "ModelCreate".

The controller "GetFile" works well, but when I check the value of "date1" and "date2" in the controller "ModelCreate" is null

I just want to make the file saved in the controller "ModelCreate"

  public string GetFile(HttpPostedFileBase file)
        {
            var fileName = this.Server.MapPath("~/Informs/" + System.IO.Path.GetFileName(file.FileName));

            if (System.IO.File.Exists(fileName))
                return "has been uploaded successfully";
            file.SaveAs(fileName);

            TempData["NameFile"] = fileName;
            TempData["File"] = file;
            return "1";
        }


        [HttpPost]
        public ActionResult ModelCreate(INFORME inform)
       {
            var date1 = TempData["NameFile"] as string;
            var date2 = TempData["File"] as HttpPostedFileBase;
            date2.SaveAs(date1);
        .
        .
        .
        .
        }

why "date1" and "date2" are null?

Blessings


Solution

  • There's not enough information to provide an answer to this question. As requested in the comments section I will provide a full example illustrating a form allowing the user to fill a couple of input fields and upload a file.

    As always we start by defining the view model which will reflect the information that we want to display on the view:

    public class MyViewModel
    {
        [Required]
        public string TextField { get; set; }
    
        [DataType(DataType.MultilineText)]
        public string TextAreaField { get; set; }
    
        public bool CheckBoxField { get; set; }
    
        [Required]
        public HttpPostedFileBase FileField { get; set; }
    }
    

    Then we could have a controller with 2 actions: a GET action that simply displays the form and a POST action that processes the form information when submitted:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel();
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            if (!ModelState.IsValid)
            {
                // there were validation errors => redisplay the view
                return View(model);
            }
    
            // at this stage the model is valid => we could do some processing
    
            // first let's save the file
            var appData = Server.MapPath("~/app_data");
            var file = Path.Combine(appData, Path.GetFileName(model.FileField.FileName));
            model.FileField.SaveAs(file);
    
            // then we could process the other properties
            // ...
    
            return Content("Thanks for submitting the data");
        }
    }
    

    and finally a strongly typed view top the view model:

    @model MyViewModel
    
    @Html.ValidationSummary(false)
    
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div>
            @Html.EditorFor(x => x.TextField)
            @Html.ValidationMessageFor(x => x.TextField)
        </div>
    
        <div>
            @Html.EditorFor(x => x.TextAreaField)
            @Html.ValidationMessageFor(x => x.TextAreaField)
        </div>
    
        <div>
            @Html.CheckBoxFor(x => x.CheckBoxField)
            @Html.ValidationMessageFor(x => x.CheckBoxField)
        </div>
    
        <div>
            @Html.LabelFor(x => x.FileField)
            @Html.TextBoxFor(x => x.FileField, new { type = "file" })
        </div>
    
        <button type="submit">OK</button>
    }