上传文件操作类

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

/// 
/// 文件上传操作
/// 
/// 邓晨露
namespace Common
{
    public class UploadFile
    {
        #region public方法
        /// 
        /// 上传图片
        /// 
        /// 控件
        /// 上传后的文件名
        /// 邓晨露
        public static string FileUploadImg(FileUpload fileUpload)
        {
            bool fileOK = false;
            string path = HttpContext.Current.Server.MapPath("~/UploadFile/Img/");
            string filename = "";

            if (fileUpload.HasFile)
            {
                string fileExtension = System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
                string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

            }
            if (fileOK)
            {
                try
                {
                    string fullfilename = fileUpload.FileName;
                    string type = fullfilename.Substring(fullfilename.LastIndexOf("."));
                    filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + type.ToString();
                    fileUpload.PostedFile.SaveAs(path + filename);

                }
                catch (Exception ex)
                {

                }
            }
            return filename;
        }
        /// 
        /// 上传视频
        /// 
        /// 控件
        /// 上传后的文件名
        /// 邓晨露
        public static string FileUploadMovie(FileUpload fileUpload)
        {
            bool fileOK = false;
            string path = HttpContext.Current.Server.MapPath("~/UploadFile/Movie/");
            string filename = "";

            if (fileUpload.HasFile)
            {
                string fileExtension = System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
                string[] allowedExtensions = { ".mp4", ".3gp", ".avi", ".mkv", ".wmv", ".mpg", ".vob", ".flv", ".swf", ".mov" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

            }
            if (fileOK)
            {
                try
                {
                    string fullfilename = fileUpload.FileName;
                    string type = fullfilename.Substring(fullfilename.LastIndexOf("."));
                    filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + type.ToString();
                    fileUpload.PostedFile.SaveAs(path + filename);

                }
                catch (Exception ex)
                {

                }
            }
            return filename;
        }
        #endregion
    }
}

你可能感兴趣的:(Asp.Net,string,exception,path,class)