zoukankan      html  css  js  c++  java
  • ASP.NET使用WebApi接口实现与Android客户端的交互(图片或字符串的接收与回传)

    最近在使用WebApi   做下记录

    //此接口实现接收Android客户端上传的JSON格式的信息,并返回“nihao”字符串

    [Route("ReceiveData")]
    [HttpPost]
    public string ReceiveData([FromBody]CarInfoView carInfoView)
    {
    using (CarInfoDbContext dbContext = new CarInfoDbContext())
    {
    CarInfoEF.Model.CarInfo carInfoEntity = new CarInfoEF.Model.CarInfo()
    {
    CarNo = carInfoView.CarNo,
    Id = 0,
    Lat = carInfoView.Lat,
    Lng = carInfoView.Lng,
    LocDt = carInfoView.LocDt,
    ReviewType = ReviewType.ReviewDefault,
    ReviewTime=DateTime.Now
    };
    dbContext.CarInfos.Add(carInfoEntity);
    dbContext.SaveChanges();
    }
    return "nihao";
    }

    //--------------------------------------------------------华丽的分割线----------------------------------------------------------------------------------------------------------------

    //此接口可以实现接收安卓上传的图片文件,并给安卓客户端返回“保存成功”的字符串

    [Route("UpdateImage")]
    [HttpPost]
    public string UpdateImage()
    {
    HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
    HttpRequestBase request = context.Request;

    if (request.Files.Count > 0)
    {
    int size = request.Files[0].ContentLength;
    byte[] fileByte = new byte[size];
    request.Files[0].InputStream.Read(fileByte, 0, size);
    var directoryPath = string.Format("D:/20170223/{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
    if (!Directory.Exists(directoryPath))
    {
    Directory.CreateDirectory(directoryPath);
    }
    var filePath = string.Format("{0}{1}", directoryPath, Path.GetExtension(request.Files[0].FileName));
    System.IO.File.WriteAllBytes(filePath, fileByte);
    return "保存成功";
    }
    else
    {
    return "没有文件";
    }
    }

    关注90後梦想大师,梦想从未止步.
  • 相关阅读:
    接口文档神器之apidoc
    ApiDoc 后端接口注释文档的使用
    Golang 数组和切片
    go切片展开
    Go的json解析:Marshal与Unmarshal
    golang depth read map
    golang 多级json转map
    GoLang中 json、map、struct 之间的相互转化
    利用delve(dlv)在Visual Code中进行go程序的远程调试-debug方式
    maximum-depth-of-binary-tree——找出数的最大深度
  • 原文地址:https://www.cnblogs.com/harveylv/p/6434715.html
Copyright © 2011-2022 走看看