码迷,mamicode.com
首页 > Windows程序 > 详细

Asp.Net或WebAPI获取表单数据流(批量文件上传)

时间:2019-09-26 14:35:17      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:保存   pos   ppa   ext   json   map   time   catch   相对   

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

            string fileName = 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
                    {
                        fileName = System.IO.Path.GetFileName(fileBase.FileName);
                        fileName = "/upload/" + DateTime.Now.ToString("yyMMddHHmmssff") + "." + fileName.Split(‘.‘)[1];
                        var filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
                        fileBase.SaveAs(filePath);//保存文件
                    }
                    catch
                    {
                        errMsg = "上传失败";
                    }

                }
            }
            return Json(new { errMsg = errMsg, fileName = fileName });
        }

Asp.Net或WebAPI获取表单数据流(批量文件上传)

标签:保存   pos   ppa   ext   json   map   time   catch   相对   

原文地址:https://www.cnblogs.com/lgq880821/p/11590634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!