【转载】FileUpload控件如何实现上传和文件属性获取

来源:互联网 发布:常用的社交软件 编辑:程序博客网 时间:2024/05/17 04:06


        protected void btnUpload_Click(object sender, EventArgs e)

        {

【方法一】

            if (FileUpload1.PostedFile.FileName == "")
            {
                Label4.Text = "请选择文件!";
            }
            else
            {
                string filepath = FileUpload1.PostedFile.FileName;
                string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
                string serverpath = Server.MapPath("image/") + filename;
                FileUpload1.PostedFile.SaveAs(serverpath);
                Label4.Text = "上传成功!";
            }

            //***********************************

【方法二】

                //if (FileUpload1.HasFile)
                //{
                //    try
                //    {
                //        int startIndex = this.FileUpload1.FileName.LastIndexOf(@"\") + 1;
                //        string fileName = this.FileUpload1.FileName.Substring(startIndex);//文件名的获取
                //        string phyFileName = this.Server.MapPath("Image") + @"\" + fileName + ".bmp";
                //        this.FileUpload1.SaveAs(phyFileName);
                //        Label4.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
                //                  "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
                //                  "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
                //                  "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" +
                //                  "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
                //                  "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
                //    }
                //    catch (System.Exception ex)
                //    {
                //        Label4.Text = "发生错误:" + ex.Message.ToString();
                //    }


                //}
                //else
                //{
                //    Label4.Text = "没有选择要上传的文件!";
                //    //message.AjaxResponeSrcipt(UpdatePanel1,this.GetType(),"提交成功");
                //}
            //} 

【方法三】
            //bool fileOK = false;
            //string path = Server.MapPath("Image");
            //if (FileUpload1.HasFile)
            //{
            //    String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
            //    for (int i = 0; i < allowedExtensions.Length; i++)
            //    {
            //        if (fileExtension == allowedExtensions[i])
            //        {
            //            fileOK = true;
            //        }
            //    }
            //}
            //if (fileOK)
            //{
            //    try
            //    {
            //        FileUpload1.SaveAs(path + FileUpload1.FileName);
            //        Label4.Text = "文件上传成功.";
            //        Label4.Text = "<b>原文件路径:</b>" + FileUpload1.PostedFile.FileName + "<br />" +
            //            "<b>文件大小:</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" +
            //            "<b>文件类型:</b>" + FileUpload1.PostedFile.ContentType + "<br />";
            //    }
            //    catch (Exception ex)
            //    {
            //        Label4.Text = "文件上传不成功.";
            //    }
            //}
            //else
            //{
            //    Label4.Text = "只能够上传图片文件.";
            //}

        }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                        华丽丽的分割线

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

 if (fileUpload.HasFile)
            {
                try
                {
                    if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("Upload") + "\\" + strDirectory)) 

                     //判断现有磁盘是否存在目录文件夹是否存在                                                                                  
                    {
                        Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("UploadFiles") + "\\" + strDirectory);

                         //如果文件夹不存在,那么新建一个文件夹
                    }
                    fileUpload.SaveAs(System.Web.HttpContext.Current.Server.MapPath("UploadFiles") 
                                    + "\\" + strDirectory + "\\" 
                                    + Path.GetFileNameWithoutExtension(fileUpload.FileName) 

               }