找回密码
 会员注册
查看: 175|回复: 0

ASP.NET使用FileUpLoad控件实现图片上传并显示

[复制链接]

250

主题

1

回帖

819

积分

管理员

积分
819
发表于 2024-2-29 08:44:49 | 显示全部楼层 |阅读模式

后端代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. namespace ex
  9. {
  10. public partial class WebForm1 : System.Web.UI.Page
  11. {
  12. string fileName; //图片名
  13. string fileExt; //图片扩展名
  14. string filePath; //图片绝对路径
  15. string fileType; //图片MIME类型
  16. double fileSize; //图片大小(byte)
  17. string[] ext = new string[] { ".png", ".jpg", ".gif" };
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. Panel1.Visible = false;
  21. }
  22. //图片上传
  23. protected void Button1_Click(object sender, EventArgs e)
  24. {
  25. if (FileUpload1.HasFile)
  26. {
  27. fileName = FileUpload1.FileName;
  28. fileExt = Path.GetExtension(fileName);
  29. fileSize = FileUpload1.PostedFile.ContentLength;
  30. fileType = FileUpload1.PostedFile.ContentType;
  31. //此处根据自己的路径修改
  32. filePath = "C:\Users\AD\source\repos\WebApplication8\WebApplication8\image" + fileName;
  33. if (fileExt == ext[0] || fileExt == ext[1] || fileExt == ext[2])
  34. {
  35. try
  36. {
  37. FileUpload1.SaveAs(Server.MapPath("./image/") + fileName);
  38. if (File.Exists(filePath))
  39. {
  40. Image1.ImageUrl = @"./image/" + fileName;
  41. Response.Write("<script>alert('上传成功!');</script>");
  42. }
  43. else
  44. Response.Write("<script>alert('图片上传成功,但图片不存在或已被删除!');</script>");
  45. }
  46. catch (Exception)
  47. {
  48. throw;
  49. }
  50. finally
  51. {
  52. Panel1.Visible = true;
  53. Label1.Text = filePath;
  54. Label2.Text = fileName;
  55. Label3.Text = fileExt;
  56. Label4.Text = Math.Round(fileSize / (1024 * 1024), 2).ToString() + "M";
  57. Label5.Text = fileType;
  58. }
  59. }
  60. else
  61. Response.Write("<script>alert('请上传png、jpg、gif格式的图片!');</script>");
  62. }
  63. else
  64. Response.Write("<script>alert('请选择要上传图片!');</script>");
  65. }
  66. }
  67. }
复制代码

前端代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="ex.WebForm1" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <!-- Bootstrap 3 核心 CSS 文件 -->
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  8. <title>image_upload</title>
  9. </head>
  10. <body>
  11. <form id="form1" runat="server">
  12. <div align="center">
  13. <h1>图片上传</h1>
  14. <asp:Image ID="Image1" runat="server" Width="300px" Height="300px"/>
  15. <asp:FileUpload ID="FileUpload1" runat="server" CssClass="btn btn-info"/>
  16. <br />
  17. <asp:Button ID="Button1" runat="server" Text="上传图片" OnClick="Button1_Click" CssClass="btn btn-primary"/>
  18. <br /><br />
  19. <asp:Panel ID="Panel1" runat="server">
  20. <table class="table table-hover table-condensed table-striped success" style="width:500px;">
  21. <tr>
  22. <td>图片路径</td>
  23. <td>
  24. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
  25. </tr>
  26. <tr>
  27. <td>图片名</td>
  28. <td>
  29. <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
  30. </tr>
  31. <tr>
  32. <td>扩展名</td>
  33. <td>
  34. <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label></td>
  35. </tr>
  36. <tr>
  37. <td>图片大小</td>
  38. <td>
  39. <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label></td>
  40. </tr>
  41. <tr>
  42. <td>图片类型</td>
  43. <td>
  44. <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label></td>
  45. </tr>
  46. </table>
  47. </asp:Panel>
  48. </div>
  49. </form>
  50. </body>
  51. </html>
复制代码

来源:https://blog.csdn.net/weixin_43553153/article/details/103301493
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-26 12:25 , Processed in 0.610645 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表