Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a partial view page,in which i have markup for file upload.I have a razor view as below inside my partial view page

@using (Html.BeginForm("Upload", "Provider", FormMethod.Post, new { id = "form", enctype = "multipart/form-data" }))
{

Select Image
<input type="file" name="file" />   
<input type="submit" value="Upload Image" name="Command" />




}

My controller method goes as

C#
[HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            // extract only the fielname
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/images/User-Image folder
            var path = Path.Combine(Server.MapPath("~/images/"), fileName);
            // this is the string you have to save in your DB
            string filepathToSave = "images/" + fileName;
            file.SaveAs(path);
            //--------------------------------------------

            // Code to save file in DB by passing the file path

            //----------------------------------------------
            return View();
        }


I always get a null value in variable "file" of controller method.I did a little bit research over this.Concluded that, in normal way if we are using file upload from a normal view instead of partial view it work's fine.But file upload work unexpectedly from a partial view page.It hits controller method as usual,but every time i get null value in variable "file" as i have mentioned earlier.I searched over google to get a solution of this issue.But failed.Please help me out.
Posted
Updated 17-Sep-13 22:09pm
v3
Comments
Michel Abi Khalil 25-Jun-14 2:51am    
Did you solve this issue? im facing the same problem :(

1 solution

C#
// Upload image And save Image At Upload Folder

using System.IO;
{
            OpenFileDialog op1 = new OpenFileDialog();
            op1.Multiselect = true;
            op1.ShowDialog();
            op1.Filter = "allfiles|*.xls";
            textBox1.Text = op1.FileName;
            pictureBox1.ImageLocation = op1.FileName;
            int count = 0;
            string[] FName;
            foreach (string s in op1.FileNames)
            {
                FName = s.Split('\\');
                File.Copy(s, "Path" + FName[FName.Length - 1]);
                count++;
            }
            MessageBox.Show(Convert.ToString(count) + " Image Uploaded");
        }



it may help u ..!!
 
Share this answer
 
Comments
Jitendra Sabat 17-Sep-13 5:21am    
Thank you for response.I need controller action method code to receive valid values in file variable.
Jitendra Sabat 17-Sep-13 5:22am    
I want to upload pdf,image and doc files for my purpose.
Neel-05 17-Sep-13 5:42am    
ohk ....

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