Request.Files相同的文件上传asp.net mvc

时间:2022-12-04 18:45:33
            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.

如果我上传多个文件,我会得到相同的文件n次。

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

我正在使用此控件:https://github.com/kartik-v/bootstrap-fileinput

My cs.html

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

Please find above my complete code.

请在上面找到我的完整代码。

3 个解决方案

#1


7  

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

而不是使用相同文件两次的foreach循环

#2


2  

According to this site,

根据这个网站,

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

从单个文件控件上载多个文件时,会为它们分配相同的名称。

#3


1  

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"/>

#1


7  

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

而不是使用相同文件两次的foreach循环

#2


2  

According to this site,

根据这个网站,

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

从单个文件控件上载多个文件时,会为它们分配相同的名称。

#3


1  

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"/>