码迷,mamicode.com
首页 > 微信 > 详细

.net core 3.1 webapi后端接收钉钉小程序post的文件/图片

时间:2020-03-04 15:12:47      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:没有   console   write   oom   操作   employees   tomcat-7   需要   efault   

原文:.net core 3.1 webapi后端接收钉钉小程序post的文件/图片

技术图片

世上本没路;走的人多了,便成了路。

技术图片
dd.uploadFile({
  url: ‘请使用自己服务器地址‘,
  fileType: ‘image‘,
  fileName: ‘file‘,
  filePath: ‘...‘,
  success: (res) => {
    dd.alert({
      content: ‘上传成功‘
    });
  },
});
技术图片

这就是官网上的例子,没有后端的。但是我猜这是用"Form"进行post提交的,那脑补后端可以在post请求的boby里获取这个文件。以下是成功的后端代码:

技术图片
 /// <summary>
        /// 用户上传头像
        /// </summary>
        /// <param name="formCollection">Boby</param>
        /// <param name="userid">userid</param>
        /// <returns></returns>
        [HttpPost, Route("postPhotoImage")]
        public StatusCodeResult UserPostPhotoImage([FromForm] IFormCollection formCollection, string userid)
        {
            if (formCollection.Files.Count > 0)
            {
                var emp = JsonConvert.DeserializeObject<EmployeeDTO>(GetEmployeeInfo(userid));
                string photo_path = @"C:\apache-tomcat-7.0.90\webapps\default\employeesProfilePicture\";
                string photo_file = photo_path + emp.jobnumber + ".jpg";
                if (System.IO.File.Exists(photo_file))
                    System.IO.File.Delete(photo_file);
                try
                {
                    #region 存储文件
                    using (FileStream fs = System.IO.File.Create(photo_file))
                    {
                        var file = formCollection.Files[0];
                        file.CopyTo(fs);
                        int photo_width = 125;
                        int photo_height = 184;
                        Image img = ZoomImage(Image.FromStream(fs), photo_height, photo_width);
                        using (MemoryStream msResult = new MemoryStream())
                        {
                            Image resultImgage = new Bitmap(photo_width, photo_height);
                            Graphics g = Graphics.FromImage(resultImgage);
                            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            g.Clear(Color.White);
                            g.DrawImage(img, new RectangleF(0, 0, photo_width, photo_height));
                            resultImgage.Save(msResult, ImageFormat.Png);
                            byte[] buffer = new byte[msResult.Length];
                            msResult.Seek(0, SeekOrigin.Begin);
                            msResult.Read(buffer, 0, buffer.Length);
                            msResult.Close();
                            fs.Write(buffer, 0, buffer.Length);
                        }
                        fs.Flush();
                        Console.WriteLine(Environment.NewLine + "=========================*===========================");
                        Console.WriteLine(DateTime.Now + "->:Upload Photo,FileName:" + fs.Name);
                        Console.WriteLine("=========================*===========================" + Environment.NewLine);
                    }
                    #endregion
                    return Ok();
                }
                catch (Exception)
                {
                    return BadRequest();
                }
            }
            else
                return BadRequest();
        }
技术图片

大伙需要注意[FromForm]标记,[FromBoby]是获取不到的!

 

技术图片
let debug_domain=‘http://cquni.vaiwan.com:8771/api/ECard‘;
let domain ="http://218.?.?.?:8771/api/ECard";
let post_url=domain+‘/postPhotoImage‘;
let photo_path=‘‘;
let current_user=‘‘;

Page({
  data: {
     src: ‘‘
  },
  onLoad(query) { 
    current_user=query.userid;
  },
  photoselect(event){  
    let that = this; 
    dd.chooseImage({
      count: 1,
      success: (res) => {
        photo_path=res.filePaths[0];
        that.setData({ 
          src:res.filePaths[0]
        });  
      },
    });
  },
  postimage(event){   
    const file_url = post_url+‘?userid=‘+current_user;  
    if (photo_path==‘‘){
      dd.alert({
        title:‘出错啦!‘,
        content: ‘请先选取或拍摄照片!预览满意后再提交上传。‘,
        buttonText:‘我晓得了‘
      });
      return;
    }
    //dd.alert({content:file_url});
    dd.uploadFile({
          url: file_url,
          fileType: ‘image‘,
          fileName: ‘employee_photo‘,
          filePath: photo_path,
          success: (res) => {
            dd.alert({
              title:‘恭喜‘,
              content: ‘操作成功,头像已上传!‘,
              buttonText:‘我晓得了‘
            });
          },
    });    
  }
});
技术图片

 

.net core 3.1 webapi后端接收钉钉小程序post的文件/图片

标签:没有   console   write   oom   操作   employees   tomcat-7   需要   efault   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12409098.html

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