【问题标题】:Request.Files same file getting uploaded asp.net mvcRequest.Files 相同的文件被上传 asp.net mvc
【发布时间】:2014-07-14 11:27:35
【问题描述】:
            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);



                }

            }

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

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

我的cs.html

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

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

【问题讨论】:

    标签: c# asp.net-mvc twitter-bootstrap file-upload


    【解决方案1】:

    改变这个:

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

    到这里:

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

    或:

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

    【讨论】:

    • 我在顶部还有其他表单元素,这可能是个问题吗?
    • 这取决于您可以通过编辑帖子添加哪些元素
    • 排除您正在使用的库应该可以正常工作,否则可能是该库导致出现问题
    • 同一文件多次发布?
    【解决方案2】:

    解决了我的问题:

    使用下面的代码

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

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

    【讨论】:

    • 谢谢,是的,foreach 循环示例在 dropzone.js MVC 教程中随处可见。感谢这个 sn-p,我相信其他人会发现它很有用。
    【解决方案3】:

    根据this site

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2011-07-08
      • 2013-06-10
      • 1970-01-01
      相关资源
      最近更新 更多