使用ASP.NET上传图片汇总(一)

1 使用标准HTML来进行图片上传

前台代码:

  1. <body>
  2.     <form id="form1" runat="server">
  3.     <div>
  4.         <table>
  5.             <tr>
  6.                 <td colspan="2" style="height: 21px" >
  7.                     使用标准HTML来进行图片上传</td>
  8.             </tr>
  9.             <tr>
  10.                 <td style="width: 400px">
  11.                     <input id="InputFile" style="width: 399px" type="file" runat="server" /></td>
  12.                 <td style="width: 80px">
  13.                     <asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" /></td>
  14.             </tr>
  15.             <tr>
  16.                 <td colspan="2" >
  17.                     <asp:Label ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>                
  18.             </tr>
  19.         </table>    
  20.     </div>
  21.     </form>
  22. </body>

后台代码:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.     }
  15.     protected void UploadButton_Click(object sender, EventArgs e)
  16.     {
  17.         string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名
  18.         //string uploadName = InputFile.PostedFile.FileName;
  19.         string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
  20.         if (InputFile.Value != "")
  21.         {
  22.             int idx = uploadName.LastIndexOf(".");
  23.             string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名
  24.             pictureName = DateTime.Now.Ticks.ToString() + suffix;
  25.         }
  26.         try
  27.         {
  28.             if (uploadName != "")
  29.             {
  30.                 string path = Server.MapPath("~/images/");
  31.                 InputFile.PostedFile.SaveAs(path + pictureName);
  32.             }
  33.         }
  34.         catch (Exception ex)
  35.         {
  36.             Response.Write(ex);
  37.         }
  38.     }
  39. }

2 单文件上传

        这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用。其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。

前台代码:

  1. <body>
  2.     <form id="form1" runat="server">
  3.     <div>
  4.         <table style="width: 90%">
  5.             <tr>
  6.                 <td style="width: 159px" colspan=2>
  7.                     <strong><span style="font-size: 10pt">最简单的单文件上传</span></strong></td>
  8.             </tr>
  9.             <tr>
  10.                 <td style="width: 600px">
  11.                     <asp:FileUpload ID="FileUpload1" runat="server" Width="600px" /></td>
  12.                 <td align=left>
  13.                     <asp:Button ID="FileUpload_Button" runat="server" Text="上传图片" OnClick="FileUpload_Button_Click" /></td>
  14.             </tr>
  15.             <tr>
  16.                 <td colspan=2>
  17.                     <asp:Label ID="Upload_info" runat="server" ForeColor="Red" Width="767px"></asp:Label></td>
  18.             </tr>
  19.         </table>    
  20.     </div>
  21.     </form>
  22. </body>

后台代码:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.     }
  15.     protected void FileUpload_Button_Click(object sender, EventArgs e)
  16.     {
  17.         try
  18.         {
  19.             if (FileUpload1.PostedFile.FileName == "")
  20.             //if (FileUpload1.FileName == "")
  21.             //if (!FileUpload1.HasFile)     //获取一个值,该值指示 System.Web.UI.WebControls.FileUpload 控件是否包含文件。包含文件,则为 true;否则为 false。
  22.             {
  23.                 this.Upload_info.Text = "请选择上传文件!";
  24.             }
  25.             else
  26.             {
  27.                 string filepath = FileUpload1.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名,如:C:/Documents and Settings/Administrator/My Documents/My Pictures/20022775_m.jpg
  28.                 //string filepath = FileUpload1.FileName;               //得到上传的文件名20022775_m.jpg
  29.                 string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);//20022775_m.jpg
  30.                 string serverpath = Server.MapPath("~/images/") + filename;//取得文件在服务器上保存的位置C:/Inetpub/wwwroot/WebSite1/images/20022775_m.jpg
  31.                 FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
  32.                 this.Upload_info.Text = "上传成功!";
  33.             }
  34.         }
  35.         catch (Exception ex)
  36.         {
  37.             this.Upload_info.Text = "上传发生错误!原因是:" + ex.ToString();
  38.         }
  39.     }
  40. }

 

   3 多文件上传

前台代码:

  1. <body>
  2.     <form id="form1" runat="server">
  3.     <div>
  4.     <table style="width: 343px">
  5.             <tr>
  6.                 <td style="width: 100px">
  7.                     多文件上传</td>
  8.                 <td style="width: 100px">
  9.                 </td>
  10.             </tr>
  11.             <tr>
  12.                 <td style="width: 100px">
  13.                     <asp:FileUpload ID="FileUpload1" runat="server" Width="475px" />
  14.                     </td>
  15.                 <td style="width: 100px">
  16.                     </td>
  17.             </tr>
  18.             <tr>
  19.                 <td style="width: 100px">
  20.                     <asp:FileUpload ID="FileUpload2" runat="server" Width="475px" /></td>
  21.                 <td style="width: 100px">
  22.                 </td>
  23.             </tr>
  24.             <tr>
  25.                 <td style="width: 100px">
  26.                     <asp:FileUpload ID="FileUpload3" runat="server" Width="475px" /></td>
  27.                 <td style="width: 100px">
  28.                 </td>
  29.             </tr>
  30.             <tr>
  31.                 <td style="width: 100px">
  32.                     <asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="一起上传" />
  33.                     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="448px"></asp:Label></td>
  34.                 <td style="width: 100px">
  35.                 </td>
  36.             </tr>
  37.         </table>
  38.     </div>
  39.     </form>
  40. </body>

 后台代码:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.     }
  15.     protected void bt_upload_Click(object sender, EventArgs e)
  16.     {
  17.         if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == "")
  18.         {
  19.             this.lb_info.Text = "请选择文件!";
  20.         }
  21.         else
  22.         {
  23.             HttpFileCollection myfiles = Request.Files;
  24.             for (int i = 0; i < myfiles.Count; i++)
  25.             {
  26.                 HttpPostedFile mypost = myfiles[i];
  27.                 try
  28.                 {
  29.                     if (mypost.ContentLength > 0)
  30.                     {
  31.                         string filepath = mypost.FileName;//C:/Documents and Settings/Administrator/My Documents/My Pictures/20022775_m.jpg
  32.                         string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);//20022775_m.jpg
  33.                         string serverpath = Server.MapPath("~/images/") + filename;//C:/Inetpub/wwwroot/WebSite2/images/20022775_m.jpg
  34.                         mypost.SaveAs(serverpath);
  35.                         this.lb_info.Text = "上传成功!";
  36.                     }
  37.                 }
  38.                 catch (Exception ex)
  39.                 {
  40.                     this.lb_info.Text = "上传发生错误!原因:" + ex.Message.ToString();
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }

 

 

                                

 

 

 

 

 

 

 

  

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值