.net 接收ios, android的上传图片
.net 接收ios, android的上传图片
二灯大师 发表于9个月前
.net 接收ios, android的上传图片
  • 发表于 9个月前
  • 阅读 49
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

总的来说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);
}

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 3
博文 74
码字总数 66184
×
二灯大师
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: