简单的asp.net文件上传类

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

namespace WebSite
{
    public class SendFile
    {
        /// <summary>
        /// 文件上传方法
        /// </summary>
        /// <param name="allowFileType">允许上传的类型,中间用","隔开</param>
        /// <param name="maxFileSize">允许上传的最大尺寸</param>
        /// <param name="fileUploadPath">上传到的目录</param>
        /// <param name="file">使用的FileUpload上传控件</param>
        /// <returns>返回文件名称</returns>
        public static string sendFileUpload(string allowFileType, int maxFileSize, string fileUploadPath, FileUpload file)
        {
            if (file.HasFile)//判断FileUpload是否有需要上传的文件
            {
                string fileName = file.FileName;//文件名
                string fileType = System.IO.Path.GetExtension(fileName);//文件类型
                int fieSize = (int)file.FileContent.Length;//文件大小
                if (allowFileType.LastIndexOf(fileType) > -1 && fieSize < maxFileSize)//判断文件类型和大小
                {
                    file.PostedFile.SaveAs(fileUploadPath + fileName);//上传文件
                    return fileName;//返回文件名
                }
                else
                {
                    return null;
                }
               
            }
            else
            {
                return null;
            }
        }
    }
}

你可能感兴趣的:(asp.net)