Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.Files same file getting uploaded asp.net mvc

            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];

                //Save file content goes here
                fName = file.FileName;
                if (file != null && file.ContentLength > 0)
                {


                    subPath = ConfigurationManager.AppSettings["SubPath"].ToString() + "/" + currentUserId;
                    bool isExists = System.IO.Directory.Exists(Server.MapPath(subPath));

                    if (!isExists)
                        System.IO.Directory.CreateDirectory(Server.MapPath(subPath));


                    string path = System.IO.Path.Combine(Server.MapPath(subPath), System.IO.Path.GetFileName(file.FileName));
                    file.SaveAs(path);



                }

            }

If i upload multiple files i get the same file n number of the times.

I am using this control: https://github.com/kartik-v/bootstrap-fileinput

My cs.html

http://codepen.io/anon/pen/aekqm

Please find above my complete code.

like image 582
Asp.net my life Avatar asked May 25 '14 16:05

Asp.net my life


3 Answers

Solved my issue:

Used the code below

for (int i = 0; i < Request.Files.Count; i++)
{
    HttpPostedFileBase file = Request.Files[i];
}

instead of the foreach loop which was taking the same file twice

like image 126
Asp.net my life Avatar answered Nov 03 '22 13:11

Asp.net my life


According to this site,

When multiple files are uploaded from a single file control, they are assigned the same name.

like image 39
Cláudio Pereira Avatar answered Nov 03 '22 12:11

Cláudio Pereira


change this:

<input id="file-3" name="files" type="file" multiple>

to this:

<input id="files" name="files" type="file" multiple="multiple"/>

or:

<input id="files" name="files" type="file" multiple="true"/>
like image 29
Ehsan Sajjad Avatar answered Nov 03 '22 12:11

Ehsan Sajjad