• 上传图片


    ///<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;

    int deviceid = HttpContext.Current.Request.Form["deviceid"] == null ? 0 : Convert.ToInt32(HttpContext.Current.Request.Form["deviceid"]);

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

                }

     

            }

  • 相关阅读:
    jQuery 插件开发——Menu(导航菜单)
    jQuery 插件开发——PopupLayer(弹出层)
    jQuery 插件开发——GridData(表格)
    angularjs+requlirejs 搭建前端框架(1)
    探讨js闭包
    python的特殊方法介绍
    收集TCP端口的访问延迟和丢包率
    【IT界的厨子】酱香鲈鱼
    Centos 6.5 安装Python 3.7
    python技巧 python2中的除法结果为0
  • 原文地址:https://www.cnblogs.com/tfeblog/p/10557579.html
Copyright © 2020-2023  润新知