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);
.
.
.
.
}
Thanks for your help, I'm new to MVC and I just want to upload a file and simultaneously send in the same form others html components for example: textbox, radio button, etc.. How I can do this? if you can help me with a complete example please...
I just want to upload a file and simultaneously send in the same form others html components for example: textbox, radio button, etc.. How I can do this?
just put
enctype="multipart/form-data"
Html.BeginForm( null, null, FormMethod.Post, new { enctype="multipart/form-data"})
and the browser will post file + others.
See http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx
CADAVID
Member
18 Points
56 Posts
Inconvenient to upload files to the server and use TempData
May 27, 2012 05:58 AM|LINK
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
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Inconvenient to upload files to the server and use TempData
May 27, 2012 06:17 AM|LINK
TempData saves into the session.
If there is enough time, the session is timed out.
More, are you testing in IIS or in Cassini?
CADAVID
Member
18 Points
56 Posts
Re: Inconvenient to upload files to the server and use TempData
May 27, 2012 06:23 AM|LINK
Hello

I'm using IIS. session time is not the problem
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Inconvenient to upload files to the server and use TempData
May 27, 2012 08:22 AM|LINK
Please replace TempData with Session.
What's the result?
CADAVID
Member
18 Points
56 Posts
Re: Inconvenient to upload files to the server and use TempData
May 27, 2012 03:21 PM|LINK
Hi friend
The result is the same. "date1" and "date2" continues null
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Inconvenient to upload files to the server and use TempData
May 27, 2012 05:48 PM|LINK
I do not think Session would not work( I have done many sites with MVC and session)
Then you have misconfigured the Session in the website . Do you have cookieless session?
CADAVID
Member
18 Points
56 Posts
Re: Inconvenient to upload files to the server and use TempData
May 28, 2012 02:55 AM|LINK
Thanks for your help, I'm new to MVC and I just want to upload a file and simultaneously send in the same form others html components for example: textbox, radio button, etc.. How I can do this? if you can help me with a complete example please...
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Inconvenient to upload files to the server and use TempData
May 28, 2012 05:10 AM|LINK
just put
enctype="multipart/form-data"Html.BeginForm( null, null, FormMethod.Post, new { enctype="multipart/form-data"})
and the browser will post file + others.
See http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx