zoukankan      html  css  js  c++  java
  • 上传文件类

    我们经常要做文件上传,而在每个页中写文件上传,会给后期维护带来很大困难,所以我们要做一个公共的类,方便我们的维护。类如下:
    using System;
    using System.IO;
    using System.Web;
    using System.Web.UI.HtmlControls;

    namespace Common
    {
        
    public class Upfile
        {
            
    private string fileType = null;
            
    private string fName = null;
            
    private string path = null;
            
    private int sizes = 0;

            
    public Upfile()
            {
                
    this.path = @"\UpLoadImages\";
                
    this.fileType = "jpg|gif|bmp";
                
    this.sizes = 3136630;
            }

            
    public  string fileSaveAs(System.Web.UI.WebControls.FileUpload name, bool creatDirectory)
            {
                
    try
                {
                    
    string tFileType;
                    
    bool flag;
                    
    string filePath = null;
                    
    string modifyFileName = 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)
                    {
                        uploadFilePath 
    = HttpContext.Current.Server.MapPath("~"+ this.path + DateTime.Now.Year.ToString() +"-"+ DateTime.Now.Month.ToString() +"-"+ DateTime.Now.Day.ToString() + @"\";
                    }
                    
    else
                    {
                        uploadFilePath 
    = HttpContext.Current.Server.MapPath("~"+ this.path;
                    }
                    
    string sourcePath = name.PostedFile.FileName;
                    
    switch (sourcePath)
                    {
                        
    case "":
                        
    case null:
                            
    return null;

                        
    default:
                            {
                                tFileType 
    = sourcePath.Substring(sourcePath.LastIndexOf("."+ 1).ToLower();
                                
    long strLen = name.PostedFile.ContentLength;
                                
    string[] temp = this.fileType.Split(new char[] { '|' });
                                flag 
    = false;
                                
    if (strLen >= this.sizes)
                                {
                                    JScript.MsgBox(
    "上传的档案不能大于" + this.sizes + "KB");
                                    
    return null;
                                }
                                
    foreach (string data in temp)
                                {
                                    
    if (data == tFileType)
                                    {
                                        flag 
    = true;
                                        
    break;
                                    }
                                }
                                
    break;
                            }
                    }
                    
    if (!flag)
                    {
                        JScript.MsgBox(
    "目前本系统支持的格式为:" + this.fileType);
                        
    //this.message("目前本系统支持的格式为:" + this.fileType);
                        return null;
                    }
                    DirectoryInfo dir 
    = new DirectoryInfo(uploadFilePath);
                    
    if (!dir.Exists)
                    {
                        dir.Create();
                    }
                    filePath 
    = uploadFilePath + modifyFileName + "." + tFileType;
                    name.SaveAs(filePath);
                    
    if (creatDirectory)
                    {
                        filePath 
    = this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + modifyFileName + "." + tFileType;
                    }
                    
    else
                    {
                        filePath 
    = this.path + modifyFileName + "." + tFileType;
                    }
                    
    this.fName = modifyFileName + "." + tFileType;
                    
    return filePath;
                }
                
    catch
                {
                   
    // this.message("");
                    
    //JScript.
                    JScript.MsgBox("出现错误,请检查图片格式!");
                    
    return null;
                }
            }

            
    public string fileSaveAs(HtmlInputFile name, bool creatDirectory)
            {
                
    try
                {
                    
    string tFileType;
                    
    bool flag;
                    
    string filePath = null;
                    
    string modifyFileName = 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)
                    {
                        uploadFilePath 
    = HttpContext.Current.Server.MapPath("~"+ this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
                    }
                    
    else
                    {
                        uploadFilePath 
    = HttpContext.Current.Server.MapPath("~"+ this.path;
                    }
                    
    string sourcePath = name.Value.Trim();
                    
    switch (sourcePath)
                    {
                        
    case "":
                        
    case null:
                            
    this.message("请选择你要上传的图片!");
                            
    return null;

                        
    default:
                            {
                                tFileType 
    = sourcePath.Substring(sourcePath.LastIndexOf("."+ 1).ToLower();
                                
    long strLen = name.PostedFile.ContentLength;
                                
    string[] temp = this.fileType.Split(new char[] { '|' });
                                flag 
    = false;
                                
    if (strLen >= this.sizes)
                                {
                                    
    this.message("上传的档案不能大于" + this.sizes + "KB");
                                    
    return null;
                                }
                                
    foreach (string data in temp)
                                {
                                    
    if (data == tFileType)
                                    {
                                        flag 
    = true;
                                        
    break;
                                    }
                                }
                                
    break;
                            }
                    }
                    
    if (!flag)
                    {
                        
    this.message("目前本系统支持的格式为:" + this.fileType);
                        
    return "";
                    }
                    DirectoryInfo dir 
    = new DirectoryInfo(uploadFilePath);
                    
    if (!dir.Exists)
                    {
                        dir.Create();
                    }
                    filePath 
    = uploadFilePath + modifyFileName + "." + tFileType;
                    name.PostedFile.SaveAs(filePath);
                    
    if (creatDirectory)
                    {
                        filePath 
    = this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + modifyFileName + "." + tFileType;
                    }
                    
    else
                    {
                        filePath 
    = this.path + modifyFileName + "." + tFileType;
                    }
                    
    this.fName = modifyFileName + "." + tFileType;
                    
    return filePath;
                }
                
    catch
                {
                    
    this.message("出现错误,请检查图片格式!");
                    
    return null;
                }
            }

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

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

            
    public string FileType
            {
                
    set
                {
                    
    this.fileType = value;
                }
            }

            
    public string FName
            {
                
    get
                {
                    
    return this.fName;
                }
                
    set
                {
                    
    this.fName = value;
                }
            }

            
    public string Path
            {
                
    set
                {
                    
    this.path = @"\" + value + @"\";
                }
            }

            
    public int Sizes
            {
                
    set
                {
                    
    this.sizes = value * 0x400;
                }
            }
        }

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    PNG文件格式具体解释
    opencv2对读书笔记——使用均值漂移算法查找物体
    Jackson的Json转换
    Java实现 蓝桥杯VIP 算法训练 装箱问题
    Java实现 蓝桥杯VIP 算法训练 装箱问题
    Java实现 蓝桥杯VIP 算法训练 单词接龙
    Java实现 蓝桥杯VIP 算法训练 单词接龙
    Java实现 蓝桥杯VIP 算法训练 方格取数
    Java实现 蓝桥杯VIP 算法训练 方格取数
    Java实现 蓝桥杯VIP 算法训练 单词接龙
  • 原文地址:https://www.cnblogs.com/Athrun/p/1031505.html
Copyright © 2011-2022 走看看