其他分享
首页 > 其他分享> > 上传图片

上传图片

作者:互联网

///<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 = "上传的文件信息不存在!" };

            }

 

        }

标签:string,ResCode,FilePath,Tpath,Request,FileName,上传,图片
来源: https://www.cnblogs.com/tfeblog/p/10557579.html