zoukankan      html  css  js  c++  java
  • Asp.Net或WebAPI获取表单数据流(批量文件上传)

    //Web或WebAPI获取表单数据流(批量文件上传)
            public JsonResult UploadFile()
            {
                //HttpPostedFileBase fileBase = Request.Files["fileToUploadKeyID"];
                HttpPostedFileBase fileBase = Request.Files[0]; //获取客户端上载的文件的集合

                string resultUrl = string.Empty;//相对文件路径
                string errMsg = string.Empty;

                if (fileBase == null || fileBase.ContentLength == 0)
                {
                    errMsg = "文件为空";
                }
                else
                {
                    int MaxSize = 1024 * 1024 * 4;
                    if (fileBase.InputStream.Length > MaxSize)
                    {
                        errMsg = "文件过大";
                    }
                    else
                    {
                        try
                        {
                            //循环遍历批量上传的文件
                            for (int i = 0; i < Request.Files.Count; i++)
                            {
                                fileBase = Request.Files[i];
                                var Name = System.IO.Path.GetFileName(fileBase.FileName);
                                var fileName = "/upload/" + DateTime.Now.ToString("yyMMddHHmmssffff") + "." + Name.Split('.')[1];
                                var filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
                                fileBase.SaveAs(filePath);//保存文件

                                resultUrl += fileName + ";";//拼接文件相对路径
                            }
                        }
                        catch
                        {
                            errMsg = "上传失败";
                        }

                    }
                }
                return Json(new { errMsg = errMsg, resultUrl = resultUrl.Trim(';') });
            }

  • 相关阅读:
    【最短路】The 2019 Asia Nanchang First Round Online Programming Contest Fire-Fighting Hero (Dijkstra)
    【积累】The 2019 Asia Nanchang First Round Online Programming Contest The Nth Item (矩阵快速幂 k进制快速幂)
    【线段树】The Preliminary Contest for ICPC Asia Xuzhou 2019 Colorful String(回文树+线段树+状压/bitset)
    allure参数说明及代码示例
    Idea+maven+testng+reportng生成测试报告
    ubuntu 16.04 镜像下载
    new AndroidDriver报错java.lang.NoSuchMethodError: com.google.common.base.Throwables.throwIfUnchecked
    Appium常用的API
    Appium常用的定位方法
    adb 指令总结
  • 原文地址:https://www.cnblogs.com/lgq880821/p/11590634.html
Copyright © 2011-2022 走看看