总的来说ios上传的是二进制, android上传的base64, 当然可以处理, 但是人家都很懒, 没办法, 刚来嘛, 只能我这边改啦
ios接收
/// <summary>
/// ios图片上传
/// </summary>
/// <returns>成功上传返回上传后的文件名</returns>
[HttpPost]
public IHttpActionResult UpLoadImage()
{
var resultMsg = new ResultMsg
{
statue = Convert.ToInt32(StatueEnum.Fail)
};
try
{
var files = HttpContext.Current.Request.Files;
if (files.AllKeys.Any())
{
foreach (string key in files.AllKeys)
{
HttpPostedFile file = files[key];
if (file != null && string.IsNullOrEmpty(file.FileName) == false)
{
var virthfolder = ConfigHelper.Instance.GetAppConfig("ServerImgaes");
var path = HttpContext.Current.Server.MapPath($"~/{virthfolder}");
var name = path + file.FileName;
file.SaveAs(name);
var fileInfo = new FileInfo(name);
var ext = Path.GetExtension(file.FileName);
var newfileName = Guid.NewGuid() + ext;
var newName = path + "/" + newfileName;
fileInfo.MoveTo(newName);
resultMsg.statue = Convert.ToInt32(StatueEnum.Success);
resultMsg.msg = International.UploadedSuccessfully;
resultMsg.data = new
{
fileName = newfileName,
url = Request.RequestUri.Authority + virthfolder, //可配,
};
}
}
}
else
{
resultMsg.msg = International.UploadEmpty;
}
}
catch (Exception exception)
{
resultMsg.statue = Convert.ToInt32(StatueEnum.Error);
resultMsg.msg = International.InternalRequestError;
}
return Ok(resultMsg);
}
android
/// <summary>
/// Android图片上传
/// </summary>
/// <returns>成功上传返回上传后的文件名</returns>
[HttpPost]
public IHttpActionResult AndroidUpLoadImage()
{
var resultMsg = new ResultMsg
{
statue = Convert.ToInt32(StatueEnum.Fail)
};
try
{
var files = HttpContext.Current.Request.Form;
if (files.Count > 0)
{
var base64Code = files["Filedata"];
var virthfolder = ConfigHelper.Instance.GetAppConfig("ServerImgaes"); //可配
var path = HttpContext.Current.Server.MapPath($"~/{virthfolder}"); //保存路径
string ext = string.Empty;
if (base64Code.Contains("data:image/jpeg;base64,"))
{
ext = ".jpeg";
base64Code = base64Code.Substring(23);
}
else if (base64Code.Contains("data:image/png;base64,"))
{
ext = ".png";
base64Code = base64Code.Substring(22);
}
else if (base64Code.Contains("data:image/jpg;base64,"))
{
ext = ".jpg";
base64Code = base64Code.Substring(22);
}
var bytes = Convert.FromBase64String(base64Code);
var ms = new MemoryStream(bytes);
var newfilename = Guid.NewGuid() + ext;
new Bitmap(ms).Save(path + "/" + newfilename);
resultMsg.statue = Convert.ToInt32(StatueEnum.Success);
resultMsg.msg = International.UploadedSuccessfully;
resultMsg.data = new
{
filename = newfilename,
url = Request.RequestUri.Authority + virthfolder, //可配,
};
}
else
{
resultMsg.msg = International.UploadEmpty;
}
}
catch (Exception exception)
{
resultMsg.statue = Convert.ToInt32(StatueEnum.Error);
resultMsg.msg = International.InternalRequestError;
}
return Ok(resultMsg);
}