现在发现两个:
一:

//设定上传文件的保存路径
string strSaveDir = "./Uploadface/";
string strName = FaceUpload.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引
int intExt = strName.LastIndexOf(".");
     
//取得文件扩展名
string strExt = strName.Substring(intExt);
     
//这里自动根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime datNow = DateTime.Now;
strName = datNow.DayOfYear.ToString() + FaceUpload.PostedFile.ContentLength.ToString() + strExt;
     
string strNewName = "ADV"+datNow.DayOfYear.ToString() + FaceUpload.PostedFile.ContentLength.ToString() + strExt;
string strPath=strSaveDir + strNewName;
     
//上传文件的类型
     
if (strExt == ".jpg" || strExt == ".gif")
{
 //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
 //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
 string path = Server.MapPath(strSaveDir + strName);
 FaceUpload.PostedFile.SaveAs(path);

 string newPath =Server.MapPath(strSaveDir + strNewName);



二:
using System;

namespace UpFile
{
    /// <summary>
    /// upfile 的摘要说明。
    /// </summary>
    public class upfile
    {
        private string path = null;
        private string fileType = null;
        private int sizes = 0;
        /// <summary>
        /// 初始化变量
        /// </summary>
        public upfile()
        {
            path = @"\uploadfile\"; //上传路径
            fileType = "jpg|gif|bmp";
            sizes = 200; //传文件的大小,默认200KB
        }

        /// <summary>
        /// 设置上传路径,如:uploadfile\
        /// </summary>
        public string Path
        {
            set
            {
                path = @"\" + value + @"\";
            }
        }

        /// <summary>
        /// 设置上传文件大小,单位为KB
        /// </summary>
        public int Sizes
        {
            set
            {
                sizes = value;
            }
            get
            {
                return sizes;
            }
        }

        /// <summary>
        /// 设置上传文件的类型,如:jpg|gif|bmp
        /// </summary>
        public string FileType
        {
            set
            {
                fileType = value;
            }           
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="name">上传控件名称</param>
        /// <param name="creatDirectory">true则以当前时间创建文件夹,false则为设置的文件夹</param>
        /// <returns>返回上传图片的相对路径</returns>
        public string fileSaveAs(System.Web.HttpPostedFile name, bool creatDirectory)
        {
           
            try
            {
                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;
                //如果为true则以当前时间创建文件夹,否则为设置的文件夹
                if (creatDirectory)
                {
                    uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + @"\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
                }
                else
                {
                    uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + path;
                }
                //获得文件的上传的路径
                string sourcePath = name.FileName.Trim();
                //判断上传文件是否为空
                if (sourcePath == "" || sourcePath == null)
                {
                    message("您没有上传数据呀,是不是搞错了呀!");
                    return null;
                }
                //获得文件扩展名
                string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
                //获得上传文件的大小
                long strLen = name.ContentLength;
                //分解允许上传文件的格式
                string[] temp = fileType.Split('|');
                //设置上传的文件是否是允许的格式
                bool flag = false;
                //判断上传文件大小
                if (strLen/1024 >= sizes)
                {

                    message("上传的图片不能大于" + sizes + "KB");
                    return null;
                }
                //判断上传的文件是否是允许的格式
                foreach (string data in temp)
                {
                    if (data == tFileType)
                    {
                        flag = true;
                        break;
                    }
                }
                //如果为真允许上传,为假则不允许上传
                if (!flag)
                {
                    message("目前本系统支持的格式为:" + fileType);
                    return null;
                }
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadFilePath);
                //判断文件夹否存在,不存在则创建
                if (!dir.Exists)
                {
                    message("程序设置的文件存放目录不存在!");
                    return null;
                    //下面这一句可以创建一个文件夹,但为了安全,设置为不创建文件夹
                    //dir.Create();
                }
                filePath = uploadFilePath + modifyFileName + "." + tFileType;
                name.SaveAs(filePath);               
                filePath = path + modifyFileName + "." + tFileType;
                //上传前使用:\目录\文件名 上传后,在网站上使用:/目录/文件名
                filePath=filePath.Replace('\\','/');
                return filePath;

            }
            catch
            {
                //异常
                message("出现未知错误!");
                return null;
            }
        }

        private void message(string msg, string url)
        {
            System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
        }

        private void message(string msg)
        {
            System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
        }
    }
}

//在程序中使用的代码为:
        //上传酒店照片
        if(酒店照片.HasFile)
        {
            UpFile.upfile UpFilePic = new UpFile.upfile();
           

            UpFilePic.Sizes = 4000;
            UpFilePic.Path = "uploadfile";
            UpFilePic.FileType = "jpg|gif|bmp|png";


            str_酒店照片 = UpFilePic.fileSaveAs(酒店照片.PostedFile, false);

            if ("" == str_酒店照片 || null == str_酒店照片)
            {
                return;
            }
        }
        else
        {
            str_酒店照片 = "";
        }

 

posted on 2006-08-04 12:19  罗明超  阅读(528)  评论(0编辑  收藏  举报