zoukankan      html  css  js  c++  java
  • 很不错的asp.net文件上传类c# 搜索文件 移动文件 删除文件等

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using System.IO;
    
    
    namespace Tool
    {
        /// <summary>
        /// file operate class
        /// </summary>
        public class FileUpload
        {
            /// <summary>
            /// Upload file
            /// </summary>
            /// <param name="path">The path of folder you want to put the file</param>
            /// <param name="UpFile">A entity of class HtmlInputFile</param>
            /// <param name="docType">Allowed file type eg:'PNG,JPG,TIF,GIF,BMP'</param>
            /// <returns>Result of upload</returns>
            //public  string Upload(string path,System.Web.UI.HtmlControls.HtmlInputFile UpFile,string docType)
            public string Upload(string path, System.Web.UI.WebControls.FileUpload UpFile, string docType)
            {
                string modifyFileName = DateTime.Now.Year.ToString();
                modifyFileName += DateTime.Now.Month.ToString();
                modifyFileName += DateTime.Now.Day.ToString();
                modifyFileName += DateTime.Now.Hour.ToString();
                modifyFileName += DateTime.Now.Minute.ToString();
                modifyFileName += DateTime.Now.Second.ToString();
                modifyFileName += DateTime.Now.Millisecond.ToString();
                string value = UpFile.FileName;
                string tFileType = value.Substring(value.LastIndexOf(".") + 1);
                string[] allowedType = docType.Split(',');
                bool TypeMatched = false;
                if (value == "" || value == null)
                {
                    return "-1";             //Error:Uploaded file is null!
                }
                else
                {
                    int FileLength = UpFile.PostedFile.ContentLength;
                    if (FileLength > (20 * 1024 * 1024))
                    {
                        return "-2";        //Error:Uploaded file is too large!
                    }
                    foreach (string type in allowedType)
                    {
                        if (type.ToLower().IndexOf(tFileType) != -1)
                        {
                            TypeMatched = true;
                        }
                    }
                    if (!TypeMatched)
                    {
                        return "-3"; //Error:Wrong type of Uploaded file!
                    }
                    string dirPath = System.Web.HttpContext.Current.Server.MapPath(path);
                    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dirPath);
                    if (!dir.Exists)
                    {
                        dir.Create();
                    }
                    path = path + modifyFileName + "." + tFileType;
                    UpFile.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path));
                    return System.Web.HttpContext.Current.Server.MapPath(path);     //Upload succeed!
                }
            }
    
            /// <summary>
            /// Search a file from the mapped path 
            /// </summary>
            /// <param name="path">The mappsed path</param>
            /// <param name="fileName">The file name for search</param>
            /// <returns>Result of search</returns>
            public int SearchFile(string path, string fileName)
            {
                System.IO.DirectoryInfo SearchDir = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(path));
                foreach (System.IO.FileInfo File in SearchDir.GetFiles())
                {
                    if (File.Name.IndexOf(fileName) != -1)
                    {
                        return 1;// Find
                    }
                }
                return 0;//Not find
            }
    
            /// <summary>
            /// Delete a file
            /// </summary>
            /// <param name="path">The path of file</param>
            /// <returns>The result of delete</returns>
            public int DeleteFile(string path)
            {
                System.IO.FileInfo File = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(path));
                if (File.Exists)
                {
                    if (File.IsReadOnly)
                    {
                        return -1;//The file is readonly
                    }
                    else
                    {
                        File.Delete();
                        return 1;//Delete succeed
                    }
                }
                else
                {
                    return 0;//The file is not exsits
                }
            }
    
            /// <summary>
            /// Move a file
            /// </summary>
            /// <param name="sFolder">source folder</param>
            /// <param name="sName">source name</param>
            /// <param name="tFolder">object folder</param>
            /// <param name="tName">object name</param>
            /// <returns></returns>
            public int FileMove(string sFolder, string sName, string tFolder, string tName)
            {
                string[] arrDirs = Directory.GetDirectories(sFolder);
                string[] arrFiles = Directory.GetFiles(sFolder);
    
                if (arrFiles.Length != 0)
                {
                    //copy this file 
                    File.Copy(sFolder + "//" + sName, tFolder + "//" + tName, true);
    
                    //delete the old file
                    return DeleteFile(sFolder + "//" + sName);
    
                }
                else
                {
                    return -1;
                }
            }
    
            /// <summary>
            /// Move selected files
            /// </summary>
            /// <param name="sFolder">source folder</param>
            /// <param name="tFolder">object folder</param>
            public void FileMove(string sFolder, string tFolder)
            {
                string[] arrDirs = Directory.GetDirectories(sFolder);
                string[] arrFiles = Directory.GetFiles(sFolder);
    
                if (arrFiles.Length != 0)
                {
    
                    for (int i = 0; i < arrFiles.Length; i++)
                    {
                        //copy these files
                        File.Copy(sFolder + "//" + Path.GetFileName(arrFiles[i]), tFolder + "//" + Path.GetFileName(arrFiles[i]), true);
    
                        //delete old files
                        DeleteFile(sFolder + "//" + Path.GetFileName(arrFiles[i]));
                    }
                }
            }
        }
    }
    

  • 相关阅读:
    pku 1061 青蛙的约会 扩展欧几里得
    莫比乌斯反演
    51Nod 1240 莫比乌斯函数
    51Nod 1284 2 3 5 7的倍数 容斥原理
    51Nod 1110 距离之和最小 V3 中位数 思维
    51Nod 1108 距离之和最小 V2 1096 距离之和最小 中位数性质
    HDU 2686 Matrix 多线程dp
    51Nod 1084 矩阵取数问题 V2 双线程DP 滚动数组优化
    HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
    设计模式(4)---单例模式
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234399.html
Copyright © 2011-2022 走看看