分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

上传图片

发布时间:2023-09-06 02:35责任编辑:蔡小小关键词:上传图片

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

            }

 

        }

上传图片

原文地址:https://www.cnblogs.com/tfeblog/p/10557579.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved