文件上传部分代码

//文件上传按钮click事件  
    protected void btnFileUpload_Click(object sender, EventArgs e)  
    {  
  
        if (FileUpLoad1.HasFile)  
        {    
           Boolean fileOk = false;
            //获取上传文件的类型
            string fileExtesion = System.IO.Path.GetExtension(upload.FileName).ToLower();
            //允许上传的类型
            string[] allowExtesions ={ ".doc", ".ppt" };
                 for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension == allowExtension[i])
                {
                    fileOk = true;
                    break;
                }
            }
            if (FileUpLoad1.PostedFile.ContentLength < 10485760 &&fileOK)  
           {  
               try  
                {                      
                //上传并指定上传目录的路径,暂定为测试文件夹  
                FileUpLoad1.SaveAs(@"D:\测试"+FileUpLoad1.FileName);  
  
                    lblMessage.Text =  "上传成功!";  
                }  
                catch (Exception ex)  
               {  
                    lblMessage.Text = "出现异常,无法上传!";  
                    //lblMessage.Text += ex.Message;  
                }  
 
            }  
            else  
            {  
               lblMessage.Text = "上传文件不能大于10MB!";  
            }  
        }  
        else  
       {  
            lblMessage.Text = "尚未选择文件!";  
       }  
    }  


测试中暂时限定文件大小为10M

posted @ 2012-12-05 23:05  MagicCode1023  阅读(278)  评论(0编辑  收藏  举报