前言

网站后台都需要有上传图片的功能,下面的例子就是实现有关图片上传。缺点:图片上传到本服务器上,不适合大量图片上传。

第一、图片上传,代码如下:

 1 xxx.aspx
 2  <td class="style1"> 
 3                 <asp:FileUpload ID="FileUpload1" runat="server"  />
 4                 <asp:Button ID="Button1" runat="server" Text="上传一般图片" onclick="Button1_Click" /> 
 5             </td>
 6             <td class="style3"> 
 7                 <asp:Image ID="Image1" runat="server" Height="200px" Width="200px" /> 
 8             </td>
 9 
10 xxx.aspx.cs
11  protected void Button1_Click(object sender, EventArgs e)
12         {
13             for (int i = 0; i < Request.Files.Count; i++)
14             {
15                 HttpPostedFile file = Request.Files[i];
16                 if (file.ContentLength > 0)
17                 {
18                     if (file.ContentType.Contains("image/"))
19                     {
20                         using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
21                         {
22                             string FileName = System.IO.Path.GetFileName(file.FileName);
23                             string[] SplitFileName = FileName.Split('.');
24                             string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss")+"." + SplitFileName[1];
25                             img.Save(Server.MapPath("/upload/" + AtterFileName));
26 
27                             this.Image1.ImageUrl = "upload/" + AtterFileName;
28                         }
29                     }
30                     else
31                     {
32                         Response.Write("<script>alert('该文件不是图片格式!');</script>");
33                     }
34                 }
35                 else
36                 {
37                     Response.Write("<script>alert('请选择要上传的图片');</script>");
38                 }
39 
40             }
41         }
View Code

相关文章:

  • 2021-07-05
  • 2021-04-16
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2021-08-12
  • 2021-06-13
  • 2022-01-08
  • 2022-03-03
  • 2022-12-23
相关资源
相似解决方案