zoukankan      html  css  js  c++  java
  • 使用MultFile.js实现多文件上传

      1.<script src="@Url.Content("~/Scripts/jquery.MultiFile.js")" type="text/javascript"></script>

    2.  <div style="padding: 10px;">
                        <input type="file" id="uploadFile" name="uploadFile" class="multi" /><br />
                        <span id="msgContent" style="color: Red;">@ViewBag.ErrorMsg</span>
          </div>

    3.[HttpPost]
            [ValidateInput(false)]
            public ActionResult AddArticle(Article article)
            {
                HttpFileCollectionBase files = Request.Files;
                string attachmentPath = string.Empty;
                if (files.Count != 0)
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        if (Path.GetExtension(Path.GetFileName(files[i].FileName)) == ".exe")
                        {
                            ViewBag.ErrorMsg = "不能上传格式为exe的文件!";
                            break;
                        }
                        if (files[i].ContentLength > 1073741824)
                        {
                            ViewBag.ErrorMsg = "文件大小最大为1G!";
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(ViewBag.ErrorMsg))
                    {
                        return View(article);
                    }
                    //上传文件
                    article.CreateDate = DateTime.Now;
                    article.LastUpdateDate = article.CreateDate;
                    articleManager.AddArticle(article);
                    IndexHelper.AddIndex(article);
                    if (files.Count == 1 && files[0].ContentLength == 0)
                    {
                        //不做操作
                    }
                    else
                    {
                        for (int i = 0; i < files.Count; i++)
                        {
                            var file = files[i];
                            //保存成自己的文件全路径,newfile就是你上传后保存的文件    
                            string fileName = System.IO.Path.GetFileName(file.FileName);//上传文件名
                            string fileExtension = System.IO.Path.GetExtension(fileName); //上传文件的扩展名
                            //保存文件
                            string FileId = DateTime.Now.ToString("yyyyMMddhhmmss");
                            string newFileName = FileId + fileExtension;
                            Attachment attachment = new Attachment
                            {
                                CreateDate = DateTime.Now,
                                FileIconName = fileExtension,
                                FileName = fileName,
                                FileSize = file.ContentLength,
                                FileUrl = newFileName
                            };
                            attachmentManager.AddAttachment(attachment);
                            articleInAttachmentManager.AddArticleInAttachment(new ArticleInAttachment { ArticleID=article.ID, AttachmentID=attachment.ID });
                            string soureFileUrl = Server.MapPath(ConfigurationManager.AppSettings["attachPath"]) + newFileName;
                            file.SaveAs(soureFileUrl);
                        }
                    }
                }
                return RedirectToAction("Index", "Home");
            }

     Web.config 

     <system.web>
        <!--上传大文件-->
        <httpRuntime maxQueryStringLength="2097151" maxUrlLength="2097151" maxRequestLength="1073741824" executionTimeout="3600" requestValidationMode="2.0"/>
      </system.web>

  • 相关阅读:
    最小生成树之算法记录【prime算法+Kruskal算法】【模板】
    [LC] 90. Subsets II
    [LC] 19. Remove Nth Node From End of List
    [LC] 125. Valid Palindrome
    [LC] 127. Word Ladder
    [LC] 102. Binary Tree Level Order Traversal
    [LC] 5. Longest Palindromic Substring
    [LC] 167. Two Sum II
    [LC] 437. Path Sum III
    [LC] 94. Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3043939.html
Copyright © 2011-2022 走看看