zoukankan      html  css  js  c++  java
  • 图片上传类

      调用:UpLoadIMG(this.FileName, "/upload/pic/", ".GIF.JPG.PNG.SWF");

    public static string TransToTwo(string theStr)
            {
                if (theStr.Length == 1)
                {
                    return "0"+theStr;
                }
                else
                {
                    return theStr;
                }
            }
           
            public static string UpLoadIMG(FileUpload fulFile, string DirectionName,string ExtName)
            {
                try
                {
                    //string FileName = fulFile.FileName;
                    string returnStr = "";

                    string FileSavePath = System.Web.HttpContext.Current.Server.MapPath("~/"); //获取文件存放的根路径

                    if (fulFile.HasFile) //判断是否选择了要上传的文件
                    {
                        string name = fulFile.PostedFile.FileName;

                        string DirectionName2 = DateTime.Now.Year.ToString() + TransToTwo(DateTime.Now.Month.ToString());

                        string DirectionName3 = TransToTwo(DateTime.Now.Day.ToString()) + TransToTwo(DateTime.Now.Hour.ToString()) + TransToTwo(DateTime.Now.Minute.ToString()) + TransToTwo(DateTime.Now.Second.ToString()) + DateTime.Now.Millisecond.ToString();

                        FileInfo file = new FileInfo(name);

                        string CreatePath="";
                        CreatePath =FileSavePath + DirectionName + DirectionName2 ;
                        FileSavePath += DirectionName + DirectionName2 +"/"+DirectionName3+ file.Extension;

                        if (ExtName.Contains( file.Extension.ToUpper() )) //符合类型
                        {
                            try
                            {
                                try
                                {
                                    if (!Directory.Exists(CreatePath))
                                    {
                                        Directory.CreateDirectory(CreatePath);
                                    }
                                }
                                catch
                                {
                                    throw;
                                }
                                fulFile.SaveAs(FileSavePath);
                            }
                            catch (Exception Error)
                            {
                                HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>");
                                return "";
                            }
                            returnStr = DirectionName2 + "/" + DirectionName3;
                      }
                        else
                        {
                            HttpContext.Current.Response.Write(" <script>alert('该文件格式不正确!') </script>");
                            return "";
                        }
                    }
                    else
                    {
                        return "";
                    }
                }
                catch
                {
                    return "";
                }
            }

  • 相关阅读:
    专访京东孙海波:大牛架构师养成记及电商供应链中区块链技术的应用(转)
    Python3 使用 matplotlib 画折线图
    JavaSE(十)之Map总结 (转)
    SciPy 安装不上?
    AI 也开源:50 大开源 AI 项目 (转)
    RabbitMQ的应用场景以及基本原理介绍 【转】
    mysql的水平拆分和垂直拆分 (转)
    MySQL在线大表DDL操作 (转)
    如何在原生工程中引入Cordova工程-for iOS 【转】
    Android 与 js 简单互调
  • 原文地址:https://www.cnblogs.com/zhang9418hn/p/2055934.html
Copyright © 2011-2022 走看看