简体   繁体   English

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

[英]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. 如果我上传多个文件,我会得到相同的文件n次。

I am using this control: https://github.com/kartik-v/bootstrap-fileinput 我正在使用此控件: https//github.com/kartik-v/bootstrap-fileinput

My cs.html 我的cs.html

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

Please find above my complete code. 请在上面找到我的完整代码。

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循环

According to this site , 根据这个网站

When multiple files are uploaded from a single file control, they are assigned the same name. 从单个文件控件上载多个文件时, 会为它们分配相同的名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM