• C#文件上传方法类(带缩略图上传方法类)


    using System;
    using System.Web;
    using System.IO; 
     /// <summary>
        /// 文件上传方法类
        /// 作者:天纯蓝
        /// 版本:0.1
        /// </summary>
        public class UpFiles
        {
            private string path = null;
            private string fileType = null;
            private int sizes = 0;
            private int status = 0;
            private int maxWith = 120;
            public UpFiles()
            {
                ///<summary>
                /// TODO: 在此处添加构造函数逻辑
                ///</summary>
                path = System.Web.HttpContext.Current.Server.MapPath("~/UpFiles"); //上传路径
                fileType = "jpg|gif|bmp";
                sizes = 200; //传文件的大小,默认200KB
                status = 0;//1是生成小图片
                maxWith = 120;//默认生成小图片最大的长度
            }
            /// <summary>
            /// 设置上传路径
            ///
            public string Path
            {
                set
                {
                    path = value ;
                }
            }
            ///<summary>
            /// 设置上传文件的类型,如:jpg|gif|bmp
            ///</summary>
            public string FileType
            {
                set
                {
                    fileType = value.ToLower();
                }
            }
            ///<summary>
            /// 设置上传文件大小,单位为KB
            ///</summary>
            public int Sizes
            {
                set
                {
                    sizes = value * 1024;
                }
            }
            ///<summary>
            /// 设置上传小图片最大宽度依准
            ///</summary>
            public int MaxWith
            {
                set
                {
                    maxWith = value;
                }
            }
            ///<summary>
            /// 设置是否生成小图片值为1生成小图片返回图片名为(小图|大图)
            ///</summary>
            public int Status
            {
                set
                {
                    status = value;
                }
            }
            ///<summary>
            /// 上传图片
            /// 上传控件名称
            /// true则以当前时间创建文件夹,false则为设置的文件夹
            /// 返回上传图片的相对路径
            /// </summary>
            public string fileSaveAs(System.Web.UI.HtmlControls.HtmlInputFile Filename, bool creatDirectory)
            {
                try
                {
                    string filePath = null;
                    string strReturnName=null;//返回的上传的文件名
                    //以当前时间修改图片的名字或创建文件夹的名字
                    string NewFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                    //获得站点的物理路径
                    string uploadFilePath = null;
                    if (creatDirectory)
                    {
                        path = path + @"\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
                        uploadFilePath = path + @"\";
                    }
                    else
                    {
                        uploadFilePath = path + @"\";
                    }
                    //获得文件的上传的路径
                    string sourceFilePath = Filename.Value.Trim();
                    if (sourceFilePath == "" || sourceFilePath == null)
                    {
                        message("您没有上传数据呀,是不是搞错了呀!");
                        return null;
                    }
                    //获得文件扩展名
                    string sEx = System.IO.Path.GetExtension(Filename.PostedFile.FileName).Replace(".", "");
                    //获得上传文件的大小
                    long postFileSize = Filename.PostedFile.ContentLength;
                    //分解允许上传文件的格式
                    string[] temp = fileType.Split('|');
                    //设置上传的文件是否是允许的格式
                    bool flag = false;
                    //判断上传文件大小
                    if (postFileSize >= sizes)
                    {

                        message("上传的文件不能大于" + sizes + "KB");
                        return null;
                    }
                    foreach (string data in temp)
                    {
                        if (data == sEx)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        message("目前本系统支持的格式为:" + fileType);
                        return null;
                    }
                    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadFilePath);
                    if (!dir.Exists)
                    {
                        dir.Create();
                    }


                    filePath = uploadFilePath + NewFileName + "." + sEx;
                    Filename.PostedFile.SaveAs(filePath);
                    filePath = path + NewFileName + "." + sEx;
                    strReturnName = NewFileName + "." + sEx;
                    if (status == 1)
                    {
                        System.Net.FileWebRequest q = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uploadFilePath + NewFileName + "." + sEx);
                        System.Net.FileWebResponse p = (System.Net.FileWebResponse)q.GetResponse();
                        int picmaxlong = maxWith;
                        System.Drawing.Image bmp = System.Drawing.Image.FromStream(p.GetResponseStream());
                        int newh;
                        float f;
                        if (bmp.Width > bmp.Height)
                        {
                            if (bmp.Width != bmp.Height)
                            {
                                f = (float)picmaxlong / (float)bmp.Width;
                                newh = (int)(f * (float)bmp.Height);
                            }
                            else
                            {
                                newh = maxWith;
                                picmaxlong = maxWith;
                            }

                        }
                        else
                        {
                            if (bmp.Width != bmp.Height)
                            {
                                f = (float)maxWith / (float)bmp.Height;
                                newh = (int)(f * (float)bmp.Width);
                                int newwh = newh;
                                newh = picmaxlong;
                                picmaxlong = newwh;
                            }
                            else
                            {
                                newh = maxWith;
                                picmaxlong = maxWith;
                            }

                        }
                        System.Drawing.Image.GetThumbnailImageAbort callb = null;
                        System.Drawing.Image image = System.Drawing.Image.FromFile(uploadFilePath + NewFileName + "." + sEx);
                        System.Drawing.Image newimage = image.GetThumbnailImage(picmaxlong, newh, callb, new System.IntPtr());
                        //把缩略图保存到指定的虚拟路径
                        newimage.Save(uploadFilePath + "small_" + NewFileName + "." + sEx);
                        //释放image对象占用的资源
                        image.Dispose();
                        //释放newimage对象的资源
                        newimage.Dispose();
                        strReturnName = "small_" + NewFileName + "." + sEx + "|" + strReturnName;
                    }
                    return strReturnName;
                }
                catch
                {
                    message("出现未知错误");
                    return null;
                }
            }
            private void message(string msg, string url)
            {
                System.Web.HttpContext.Current.Response.Write("<script language='javascript'> alert('" + msg + "');window.location='" + url + "'; </script>");
            }

            private void message(string msg)
            {
                System.Web.HttpContext.Current.Response.Write("<script language='javascript'> alert('" + msg + "'); </script>");
            }

        }

       调用方法:
    例下 
           UpFiles upFiles = new UpFiles();
            upFiles.FileType = "gif|jpg|bmp";
            upFiles.Sizes = 1500;
            upFiles.Path = System.Web.HttpContext.Current.Server.MapPath("~/UpFiles");
            upFiles.Status = 0;
            string picName = upFiles.fileSaveAs(files, false);

     

  • 相关阅读:
    String源码分析
    solr IK分词器
    solr安装
    hadoop HA集群搭建(亲测)
    dubbo-admin安装
    关于idea中使用lamb表达式报错:ambda expressions are not supported at this language level
    web项目数据存入mysql数据库中文乱码问题
    dom4j解析xml
    js监听键盘提交表单
    Location replace() 方法
  • 原文地址:https://www.cnblogs.com/skyblue/p/674606.html
Copyright © 2020-2023  润新知