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 "没有文件";
}
}

上一篇:mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题


下一篇:CMD删除Mysql 服务