星空网 > 软件开发 > ASP.net

对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 = "文件类型不符合";       }    }   }

  

做时间的主人,命运的主宰,灵魂的舵手。



原标题:对FileUpload文件上传控件的一些使用方法说明

关键词:上传

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Chen Xin:https://www.goluckyvip.com/tag/17014.html
chengyipets:https://www.goluckyvip.com/tag/17015.html
chenshufang05:https://www.goluckyvip.com/tag/17016.html
chenshufang06:https://www.goluckyvip.com/tag/17017.html
CHENXIN:https://www.goluckyvip.com/tag/17018.html
chiclover:https://www.goluckyvip.com/tag/17019.html
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
百崖大峡谷生态旅游景区(探秘中国西南自然风光):https://www.vstour.cn/a/363176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流