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;
                }
            }
        }
    }
  • 相关阅读:
    json数组去重
    java socket API
    java网络编程精解demo1---读取用户控制台的输入的数据并显示
    【CodeForces 489A】SwapSort
    【CSU 1556】Pseudoprime numbers
    【CodeForces 472A】Design Tutorial: Learn from Math
    【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E
    【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905
    【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree
    【UVA 401】BUPT 2015 newbie practice #2 div2-B-Palindromes
  • 原文地址:https://www.cnblogs.com/xbf321/p/897231.html
Copyright © 2011-2022 走看看