Web文件(图片)上传方法

在开放Web应用程序的时候经常会遇到图片或者是文件上传的模块,这里就是该模块的实现的后台方法

上传图片方法

/// <summary>
        /// 功能:上传图片方法
        /// </summary>
        /// <param name="moduleType">运营模块枚举类型</param>
        /// <param name="toFileApplicationPath">mappath指定的要保存的路径</param>
        /// <param name="toShowApplictionFilePath">客户端显示相对路径</param>
        /// <param name="hpf">文件域对象</param>
        /// <returns>返回处理结果(这里处理结果是以json形式表示)</returns>
        public static void UploadImage(ModuleType moduleType, HttpPostedFile hpf, string toFileApplicationPath, string toShowApplictionFilePath)
        {
            //json字符串
            string jsonStr = string.Empty;
            //生成要保存的文件名
            string fileName = string.Empty;
            //完整路径
            string toFile = string.Empty;
            try
            {
                if (hpf.ContentLength < 2087052)
                {
                    if (!string.IsNullOrEmpty(hpf.FileName))
                    {
                        //获取文件扩展名
                        string fileNameExt = Path.GetExtension(hpf.FileName).ToLower().Trim();
                        if (CheckImageExt(fileNameExt))
                        {
                            switch (moduleType)
                            {
                                //产品图片
                                case ModuleType.product:
                                    fileName = CreateFileName(ModuleType.product);
                                    break;
                                //logo图片
                                case ModuleType.logo:
                                    fileName = CreateFileName(ModuleType.logo);
                                    break;
                                //幻灯片
                                case ModuleType.slides:
                                    fileName = CreateFileName(ModuleType.slides);
                                    break;
                                case ModuleType.newsIndexImg:
                                    fileName = CreateFileName(ModuleType.newsIndexImg);
                                    break;
                                case ModuleType.customer:
                                    fileName = CreateFileName(ModuleType.customer);
                                    break;
                                case ModuleType.productCategory:
                                    fileName = CreateFileName(ModuleType.productCategory);
                                    break;
                                case ModuleType.productCategoryIndex:
                                    fileName = CreateFileName(ModuleType.productCategoryIndex);
                                    break;
                                case ModuleType.server:
                                    fileName = CreateFileName(ModuleType.server);
                                    break;
                                case ModuleType.memberHeadImg:
                                    fileName = CreateFileName(ModuleType.memberHeadImg);
                                    break;
                                case ModuleType.caseInfo:
                                    fileName = CreateFileName(ModuleType.caseInfo);
                                    break;
                                case ModuleType.caseMultiImg:
                                    fileName = CreateFileName(ModuleType.caseMultiImg);
                                    break;
                                case ModuleType.payment:
                                    fileName = CreateFileName(ModuleType.payment);
                                    break;
                                case ModuleType.player:
                                    fileName = CreateFileName(ModuleType.player);
                                    break;
                                case ModuleType.productMultiImg:
                                    fileName = CreateFileName(ModuleType.productMultiImg);
                                    break;
                                case ModuleType.teacherImg:
                                    fileName = CreateFileName(ModuleType.teacherImg);
                                    break;
                                default:
                                    break;
                            }
                            //物理完整路径
                            string toFileFullPath = HttpContext.Current.Server.MapPath(toFileApplicationPath);
                            //检查是否有该路径,没有就创建
                            if (!Directory.Exists(toFileFullPath))
                            {
                                Directory.CreateDirectory(toFileFullPath);
                            }
                            //获得要保存的相对路径
                            string applicationFilePath = toShowApplictionFilePath + fileName + fileNameExt;
                            //得大始图像对象(如果用户上传的图片比较需求规定的最小规格还小则不给于缩略图处理)
                            toFile = toFileFullPath + fileName + fileNameExt;
                            hpf.SaveAs(toFile);
                            HttpContext.Current.Response.ContentType = "text/HTML";
                            string showClient = toShowApplictionFilePath + fileName + fileNameExt;
                            jsonStr = "{\"msg\":\"success\",\"imgPath\":\"" + showClient + "\",\"golobImgDesc\":\"" + "" + "\",\"dataImgPath\":\"" + applicationFilePath + "\",\"showClient\":\"" + showClient + "\"}";
                            HttpContext.Current.Response.Write(jsonStr);
                        }
                        else
                        {
                            HttpContext.Current.Response.ContentType = "text/HTML";
                            jsonStr = "{\"msg\":\"errorExtens\"}";
                            HttpContext.Current.Response.Write(jsonStr);
                        }
                    }
                    else
                    {
                        HttpContext.Current.Response.ContentType = "text/HTML";
                        jsonStr = "{\"msg\":\"notUploadFile\"}";
                        HttpContext.Current.Response.Write(jsonStr);
                    }
                }
                else
                {
                    HttpContext.Current.Response.ContentType = "text/HTML";
                    jsonStr = "{\"msg\":\"imgBig2M\"}";
                    HttpContext.Current.Response.Write(jsonStr);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
View Code

上传文件方法

        /// <summary>
        /// 功能:上传文件方法
        /// </summary>
        /// <param name="moduleType">运营模块枚举类型</param>
        /// <param name="toFileApplicationPath">mappath指定的要保存的路径</param>
        /// <param name="toShowApplictionFilePath">客户端显示相对路径</param>
        /// <param name="hpf">文件域对象</param>
        /// <returns>返回处理结果(这里处理结果是以json形式表示)</returns>
        public static void UploadFile(ModuleType moduleType, HttpPostedFile hpf, string toFileApplicationPath, string toShowApplictionFilePath)
        {
            //json字符串
            string jsonStr = string.Empty;
            //生成要保存的文件名
            string fileName = string.Empty;
            //完整路径
            string toFile = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(hpf.FileName))
                {
                    //获取文件扩展名
                    string fileNameExt = Path.GetExtension(hpf.FileName).ToLower().Trim();
                    if (CheckFileExt(fileNameExt))
                    {
                        switch (moduleType)
                        {
                            case ModuleType.productFile:
                                fileName = CreateFileName(ModuleType.productFile);
                                break;
                            case ModuleType.adFile:
                                fileName = CreateFileName(ModuleType.adFile);
                                break;
                            case ModuleType.downloadFile:
                                fileName = CreateFileName(ModuleType.downloadFile);
                                break;
                            default:
                                break;
                        }
                        //物理完整路径
                        string toFileFullPath = HttpContext.Current.Server.MapPath(toFileApplicationPath);
                        //检查是否有该路径,没有就创建
                        if (!Directory.Exists(toFileFullPath))
                        {
                            Directory.CreateDirectory(toFileFullPath);
                        }
                        //获得要保存的相对路径
                        string applicationFilePath = toShowApplictionFilePath + fileName + fileNameExt;
                        toFile = toFileFullPath + fileName + fileNameExt;
                        hpf.SaveAs(toFile);
                        HttpContext.Current.Response.ContentType = "text/HTML";
                        string showClient = toShowApplictionFilePath + fileName + fileNameExt;
                        jsonStr = "{\"msg\":\"success\",\"imgPath\":\"" + showClient + "\",\"golobImgDesc\":\"" + "" + "\",\"filePath\":\"" + applicationFilePath + "\",\"showClient\":\"" + showClient + "\"}";
                        HttpContext.Current.Response.Write(jsonStr);
                    }
                    else
                    {
                        HttpContext.Current.Response.ContentType = "text/HTML";
                        jsonStr = "{\"msg\":\"errorExtens\"}";
                        HttpContext.Current.Response.Write(jsonStr);
                    }
                }
                else
                {
                    HttpContext.Current.Response.ContentType = "text/HTML";
                    jsonStr = "{\"msg\":\"notUploadFile\"}";
                    HttpContext.Current.Response.Write(jsonStr);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

是否为合法的图片

     /// <summary>
     /// 功能:检查是否为合法的上传文件
        /// </summary>
        /// <param name="fileExt"></param>
        /// <returns></returns>
        public static bool CheckImageExt(string fileExt)
        {
            string[] allowExt = new string[] { ".gif", ".jpg", ".jpeg", ".png" };
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i].ToLower().Trim() == fileExt.ToLower().Trim()) { return true; }
            }
            return false;
        }

检查是否为合法的上传文件

/// <summary>
/// 功能:检查是否为合法的上传文件
/// </summary>
/// <param name="fileExt"></param>
/// <returns></returns>
        public static bool CheckFileExt(string fileExt)
        {
            string[] allowExt = new string[] { ".pdf", ".doc", ".zip",".rar" };
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i].ToLower().Trim() == fileExt.ToLower().Trim()) { return true; }
            }
            return false;
        }

 

转载于:https://www.cnblogs.com/antsong/p/ant_song.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值