ASP.NET 上传文件到服务器

来源:互联网 发布:网络剧有毒在线观看 编辑:程序博客网 时间:2024/04/30 07:05

        string SysURL = Server.MapPath("../DCO_PIC");
        File_Model fm = FileUp(this.FileUpload6, SysURL, "Pic");   //  1.FileUpload控件  2.上传目录地址  3.上传文件类型

 

 #region  上传文件方法
    public static File_Model FileUp(FileUpload fileUp, string SysURL,string type)
    {
        File_Model fm = new File_Model();
        if (fileUp.PostedFile.FileName == "")
        {
            return fm;
        }

        //   /获取文件信息
        string FileName = fileUp.PostedFile.FileName;
        string picExten = System.IO.Path.GetExtension(FileName);
        if (type == "Pic")
        {
            if (picExten != ".png" && picExten != ".jpg" && picExten != ".gif" && picExten != ".bmp")
            {
                return fm;
            }
        }
        else if (type == "Video")
        {
            if (picExten != ".avi" && picExten != ".RM" && picExten != ".RMVB" && picExten != ".AVI" && picExten != ".WMV" && picExten != ".MPG "

            && picExten != ".MPEG" &&     picExten != ".3GP" && picExten != ".MP4 " && picExten != ".SWF")
            {
                return fm;
            }
        }
        string file_str = "文件名称:" + FileName + "<br>";
        file_str = "文件类型:" + fileUp.PostedFile.ContentType + "<br>";
        file_str = "文件长度:" + fileUp.PostedFile.ContentLength.ToString() + "KB<br>";
        DirectoryInfo upDir = new DirectoryInfo(SysURL);
        if (!upDir.Exists)
        {
            upDir.Create();
        }
        //上传文件到服务器
        FileName = FileName.Substring(FileName.LastIndexOf("\\") + 1);// 取出文件名的路径(不包括文件的名称)
        string endFileName = DateTime.Now.ToString("yyyyMMddHHmm") + "_" + FileName;
        string upload_file = SysURL + "/" + endFileName;
        //取出服务器虚拟路径,存储上传文件
        fileUp.PostedFile.SaveAs(upload_file);//开始上传文件
        string updateTime = DateTime.Now.ToString();
        fm.FileUrl = endFileName;
        return fm;
    }

    #endregion

 

//文件实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace  FileModel
{
    public class File_Model
    {

        /// <summary>
        /// FileID
        /// </summary>
        private Guid _FileID;
        public Guid FileID
        {
            set { _FileID = value; }
            get { return _FileID; }
        }
        /// <summary>
        /// FileName
        /// </summary>
        private string _FileName;
        public string FileName
        {
            set { _FileName = value; }
            get { return _FileName; }
        }

        /// <summary>
        /// FileName
        /// </summary>
        private DateTime _UpTime;
        public DateTime UpTime
        {
            set { _UpTime = value; }
            get { return _UpTime; }
        }
        /// <summary>
        /// FileUrl
        /// </summary>
        private string _FileUrl;
        public string FileUrl
        {
            set { _FileUrl = value; }
            get { return _FileUrl; }
        }
     
    }
}

 

原创粉丝点击