zoukankan      html  css  js  c++  java
  • 对FileUpload文件上传控件的一些使用方法说明

    //创建时间:2014-03-12

    //创建人:幽林孤狼

    //说明:FileUpload文件上传控件使用说明(只是部分)已共享学习为主

    //可以上传图片,txt文档。doc,wps,还有音频文件,视屏文件等,功能强大啊!

    //前台代码片,设置上传图片的界面

    <div>        

    <asp:Label ID ="lbText" runat ="server" Font-Bold ="true" Font-Size ="20px" Text ="请选择您要上传的照片">  

     </asp:Label>        

    <br /><asp:Label ID="lbImg" runat ="server" ForeColor ="Red" ></asp:Label><br />    

     <asp:Label ID="hh" runat ="server" Text ="上传路径:"></asp:Label><br />        

    <asp:TextBox ID="txtFileName" runat ="server" ></asp:TextBox>        

    <asp:FileUpload ID="FileUpLoad1" runat ="server" />        

    <asp:Button ID="btnUpload" runat ="server" Text ="开始上传" OnClick="btnUpload_Click" />  <br />        

    <asp:Label ID="hj" runat ="server" Text ="上传的图片如下"></asp:Label>        

    <br />   <asp:Image ID="img" runat ="server" />       

    </div>

    //后台代码 ,实现图片从客户端传到服务器  

    protected void btnUpload_Click(object sender, EventArgs e)   

      {         //HasFile 获取一个值,该值指示是否在 FileUpload 控件包含一个文件。         

               if (FileUpLoad1 .HasFile)        

         {  

             //PostedFile通过使用 FileUpload 控件上载的文件获取的基础的 HttpPostedFile 对象。public HttpPostedFile   

               //PostedFile { get; }  

               string fileContentType = FileUpLoad1.PostedFile.ContentType;  

               //判断文件类型 当然也可以用file.Extension进行判断,如:if(file.Extension!=".txt"){}

                 if (fileContentType == "image/jpeg" || fileContentType == "image/bmp" || fileContentType == "image/gif" ||  

           fileContentType == "image/png")

                 {

                    //客户端文件路径

                    string name = FileUpLoad1.PostedFile.FileName;

                    FileInfo file = new FileInfo(name);  

                   //文件名称

                    string filename = file.Name;

                    //服务器端文件路径  

                   this.txtFileName.Text = filename;  

                   string webFilePath = Server.MapPath("Upload/"+filename );

                    //判断相同文件是否存在

                    if (!File.Exists (webFilePath))

                    {

                        try  {

                           //使用SqveAs()在 Web 服务器上将上载的文件的内容保存到指定的路径

                            FileUpLoad1.SaveAs(webFilePath );

                            this.lbImg.Text = "提示:文件“"+filename+"”上传成功!";

                            this.img.ImageUrl = "Upload/"+filename ;

                        }

                        catch (Exception ex) {

                            this.lbImg.Text = "文件上传失败,原因是"+ex.Message ;

                        }

                    }

                   

                }

                else     

                {                 this.lbImg.Text = "文件类型不符合";    

             }

            }  

       }

  • 相关阅读:
    游标cursor
    SQL: EXISTS
    LeetCode Reverse Integer
    LeetCode Same Tree
    LeetCode Maximum Depth of Binary Tree
    LeetCode 3Sum Closest
    LeetCode Linked List Cycle
    LeetCode Best Time to Buy and Sell Stock II
    LeetCode Balanced Binary Tree
    LeetCode Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/ylgl/p/3597272.html
Copyright © 2011-2022 走看看