zoukankan      html  css  js  c++  java
  • asp.net webapi 多文件上传

    使用enctype="multipart/form-data"来进行操作

            /// <summary>
            /// 上传图片
            /// </summary>
            /// <returns></returns>
            [HttpPost]
            [Token]
            [Route("User/Upload")]
            public async Task<HttpResponseMessage> Upload()
            {
                APIUpload apiUpload = new APIUpload();
                var provider = await Request.Content.ReadAsMultipartAsync();
                List<string> uploadedImages = new List<string>(provider.Contents.Count);
                foreach (var item in provider.Contents)
                {
                    if (item.Headers.ContentDisposition.FileName != null)
                    {
                        var data = await item.ReadAsByteArrayAsync();
                        //将流存放到阿里oss
                        string imageUrl = apiUpload.SavePlayImage(data);
                        ZBBlackboard blackboard = new ZBBlackboard();
                        uploadedImages.Add(imageUrl);
                    }
                }
                List<ZBBlackboard> blackboards = new List<ZBBlackboard>(uploadedImages.Count);
                using (CMSDataContext context = DataContextFactory.CreateCMS())
                {
                    int maxId = 0;
                    if (context.ZBBlackboard.Count() != 0)
                    {
                        maxId = context.ZBBlackboard.Max(b => b.ID);
                    }
                    foreach (string imageUrl in uploadedImages)
                    {
                        ZBBlackboard blackboard = new ZBBlackboard();
                        blackboard.ID = ++maxId;
                        blackboard.ImgUrl = imageUrl;
                        blackboards.Add(blackboard);
                    }
                    context.ZBBlackboard.InsertAllOnSubmit<ZBBlackboard>(blackboards);
                    context.SubmitChanges();
                }
                string token = LoginStatusHelper.GetNewLoginStatus(this.Request);
                return this.Request.CreateResponse<APIResponseData>(APIResponseData.GetResponse(200, data: new { Images = blackboards }, token: token));
            }
    
  • 相关阅读:
    MCU软件最佳实践——独立按键
    MCU软件最佳实践——矩阵键盘驱动
    MCU软件最佳实践——使用printf打印数据
    CAP定理图解证明
    类型和变量
    数字ID过长 精度丢失 (已解决:后端方案)
    Springboot 异步线程池配置(小型应用)
    Java 数字 字符串 简单操作
    Java 网络请求
    Java 时间 日 周 月 季 年
  • 原文地址:https://www.cnblogs.com/grj1046/p/4623949.html
Copyright © 2011-2022 走看看