#region 上传文件的方法
/// <summary>
/// 上传文件方法
/// </summary>
/// <param name="myFileUpload">上传控件ID</param>
/// <param name="allowExtensions">允许上传的扩展文件名类型,如:string[] allowExtensions = { ".doc", ".xls", ".ppt", ".jpg", ".gif" };</param>
/// <param name="maxLength">允许上传的最大大小,以M为单位</param>
/// <param name="savePath">保存文件的目录,注意是绝对路径,如:Server.MapPath("~/upload/");< /param>
/// <param name="saveName">保存的文件名,如果是'""'则时间为文件名保存</param>
public static void Upload(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath, string saveName)
{
// 文件格式是否允许上传
bool fileAllow = false;
//检查是否有文件案
if (myFileUpload.HasFile)
{
// 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024
if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
{
throw new Exception("上传文件超过允许上传容量!");
}

//取得上传文件之扩展文件名,并转换成小写字母
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
string tmp = "";   // 存储允许上传的文件后缀名
//检查扩展文件名是否符合限定类型
for (int i = 0; i < allowExtensions.Length; i++)
{
tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";
if (fileExtension == allowExtensions[i])
{
fileAllow = true;
}
}

if (fileAllow)
{
string myTime = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString("000");
DirectoryInfo myFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(savePath));

if (!myFolder.Exists)
{ myFolder.Create(); }

try
{
string path = System.Web.HttpContext.Current.Server.MapPath(savePath +"/"+ (saveName == "" ? myTime : saveName));
//存储文件到文件夹
myFileUpload.SaveAs(path);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);
}
}
else
{
throw new Exception("请选择要上传的文件!");
}
}

//以下是测试的代码:
//try
//  {
//     string[] ss = { ".jpg", ".gif" };
//     Jproc.Jupload.Upload(FileUpload1, ss, 1, "aaaaaaa", "");
//     Label1.Text = "文件上传成功!";
//  }
//  catch (Exception ex)
//  {
//     Label1.Text =ex.Message;
//  }
#endregion

 


图片上传

----------------------------------------

 

protected void Button2_Click(object sender, EventArgs e)

      {

         if (FileUpload1.HasFile)

         {

            //要考虑文件大小

            string filetype = FileUpload1.PostedFile.ContentType.ToLower();

 

            if (filetype == "image/gif" || filetype == "image/jpeg")

            {

               string myTime;

               myTime = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString("000");

               string gfile = FileUpload1.PostedFile.FileName;

               string pic_path = Server.MapPath("/upfile/other/"); // 上传文件夹

               //判断上传文件夹是否存在,不存在则创建

               DirectoryInfo myFolder = new DirectoryInfo(pic_path);

               if (!myFolder.Exists)

               { myFolder.Create(); }

 

               FileInfo MyFile = new FileInfo(gfile);

               string save_filePath = Server.MapPath("/upfile/other/" + myTime + MyFile.Extension);

 

 

               ////判断子上传文件夹,不存在则按年月日创建文件夹

               //  myFolder = new DirectoryInfo(pic_path + "/" + date_folder);

               //  if (!myFolder.Exists)

               //  { myFolder.Create(); }

               FileUpload1.SaveAs(save_filePath); //上传文件

               FCKeditor1.Value += "<img src='/upfile/other/" + myTime + MyFile.Extension + "' />";//上传的文件添加到FCK编辑器中

 

               //Response.Write("原文件名:" + MyFile.Name + "  文件大小:" + MyFile.Length / 1024 + "kb  后缀:" + MyFile.Extension);

            }

            else

            { Response.Write("<script>alert('类型不符')</script>"); }

         }

      }

 

摘自:http://hi.baidu.com/wenjunlin/blog/item/f600ef01e74a870c1d958383.html


/////////////////////////////////////////

获取后缀的简单方法

string houzhui=System.IO.Path.GetExtension(fileupload1.PostedFile.FileName).ToLower();

posted on 2010-11-12 21:31  露水丛生  阅读(699)  评论(0编辑  收藏  举报