在ASP.NET MVC中的jQuery ajax中使用多个文件上载时,会话中的双文件上载

时间:2017-02-21 07:17:24

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

我使用此代码在jQuery ajax中使用ASP.NET MVC进行多次上传:

public ActionResult GetFiles(HttpPostedFileBase NewsFilePath)
{
    if (NewsFilePath != null)
    {
        var files = Session["Files"] == null ?
            new List<HttpPostedFileBase>() :
            (List<HttpPostedFileBase>)Session["Files"];
        files.Add(NewsFilePath);
        Session["Files"] = files;
    }
    return Content("");
}

但我对此代码有疑问。

  • 当我上传一个文件时,会有一个文件存储到会话中
  • 当我上传两个文件时,4个文件存储在会话中
  • 当我上传三个文件时,会有6个文件存储到会话中

问题是什么?

1 个答案:

答案 0 :(得分:0)

在将文件分配到.Distinct()

时使用Session
    public ActionResult GetFiles(HttpPostedFileBase NewsFilePath)
    {
        if (NewsFilePath != null)
        {
            var files = Session["Files"] == null ?
                new List<HttpPostedFileBase>() :
                (List<HttpPostedFileBase>)Session["Files"];
            files.Add(NewsFilePath);
            Session["Files"] = files.Distinct();
        }
        return Content("");
    }

希望它会解决您的问题,如果没有,请在下面发表评论。