• 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(';') });
            }

  • 相关阅读:
    Taskbar missing in ubuntu 10.04
    Ubuntu中如何安装*.sty文件(TeTeX或Tex Live)
    Install Fcitx on Ubuntu
    ROS(Robot Operating System)维基百科页面发布了!
    我的fedora,崩溃了。
    分享一个小巧简单的基金查询工具(自己写的)
    软件说明书——基于V0.2.2
    [linux笔记]火狐扩增从windwos导到ubuntu。
    [Linux笔记]下载软件选择篇
    [linux笔记]第一次工作上用了平时学习的东西。
  • 原文地址:https://www.cnblogs.com/lgq880821/p/11590634.html
Copyright © 2020-2023  润新知