.net 接收ios, android的上传图片

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

 

转载于:https://my.oschina.net/objectboy/blog/1525958

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值