上传图片

///<summary>

/// 上传图片  api接口

///</summary>

///<returns></returns>

        [Route("api/User/UploadImg/")]

        [HttpPost]

publicUploadImgResult UploadImg()

        {

string FileExt = "";//后缀名

string FileName = "";//重命名

string FilePath = "";//文件路径

string Tpath = "";

string ResultPath = "";//返回图片路径

 

HttpFileCollection filelist = HttpContext.Current.Request.Files;

if (filelist != null&& filelist.Count > 0)

            {

try

                {

for (int i = 0; i < filelist.Count; i++)

                    {

HttpPostedFile file = filelist[i];

                        Tpath = "" + DateTime.Now.ToString("yyyy-MM-dd") + "";

                        FileExt = file.FileName.Substring(file.FileName.LastIndexOf('.')).ToUpper();

                        FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");

                        FilePath = HttpContext.Current.Server.MapPath("~/AdFile/" + Tpath + "/");

//FilePath = "http://" + Request.RequestUri.Host + ":" + Request.RequestUri.Port + "/AdWebApi/AdFile/" + Tpath + "/";

DirectoryInfo di = newDirectoryInfo(FilePath);//创建文件夹对象

 

if (!di.Exists) { di.Create(); }//判断文件夹是否存在

                        file.SaveAs(FilePath + FileName + FileExt);

                    }

 

                    ResultPath = "http://" + Request.RequestUri.Host + ":" + Request.RequestUri.Port + "/AdWebApi/AdFile/" + Tpath + "/" + FileName + FileExt;

returnnewUploadImgResult { ResCode = ResCode.Ok, ResMsg = "上传成功", imagePath = ResultPath };

                }

catch (Exception ex)

                {

                    logs.Error(ex.ToString());

returnnewUploadImgResult { ResCode = ResCode.Fail, ResMsg = ex.Message };

                }

            }

else

            {

returnnewUploadImgResult { ResCode = ResCode.NoData, ResMsg = "上传的文件信息不存在!" };

            }

 

        }

上一篇:STM32F0单片机快速入门一: ARM架构与STM32F0


下一篇:聊聊CMSIS-RTOS是什么东东