现在的位置: 首页 > 综合 > 正文

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

2013年10月22日 ⁄ 综合 ⁄ 共 4666字 ⁄ 字号 评论关闭

 

4 客户端检查上传文件类型(以上传图片为例)

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
    %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>客户端检查上传文件类型</title>
  6. <script language="javascript">
  7. function Check_FileType()
  8. {
  9. var str=document.getElementById("FileUpload1").value;
  10. var pos=str.lastIndexOf(".");
  11. var lastname=str.substring(pos,str.length);
  12. if(lastname.toLowerCase()!=".jpg"&&lastname.toLowerCase()!=".gif")
  13. {
  14. alert("您上传的文件类型为"+lastname+",图片必须为.jpg,.gif类型");
  15. return false;
  16. }
  17. else
  18. {
  19. return true;
  20. }
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <form id="form1" runat="server">
  26. <div>
  27. <table>
  28. <tr>
  29. <td colspan="2">
  30. 客户端检查上传文件类型</td>
  31. </tr>
  32. <tr>
  33. <td style="width: 444px">
  34. <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
  35. <td style="width: 80px">
  36. <asp:Button ID="bt_upload" runat="server" Text="上传图片" OnClick="bt_upload_Click"
    OnClientClick=
    "return Check_FileType()" /></td>
  37. </tr>
  38. <tr>
  39. <td colspan="2" style="height: 21px">
  40. <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>
  41. </tr>
  42. </table>
  43. </div>
  44. </form>
  45. </body>
  46. </html>

注意:点击上传时先触发客户端事件OnClientClick="return Check_FileType()"

  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. try
  18. {
  19. if (FileUpload1.PostedFile.FileName ==
    "")
  20. {
  21. this.lb_info.Text =
    "请选择文件!";
  22. }
  23. else
  24. {
  25. string filepath = FileUpload1.PostedFile.FileName;
  26. //if (!IsAllowedExtension(FileUpload1))
  27. //{
  28. // this.lb_info.Text = "上传文件格式不正确!";
  29. //}
  30. if (IsAllowedExtension(FileUpload1) ==
    true)
  31. {
  32. string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
  33. string serverpath = Server.MapPath("~/images/") + filename;
  34. FileUpload1.PostedFile.SaveAs(serverpath);
  35. this.lb_info.Text =
    "上传成功!";
  36. }
  37. else
  38. {
  39. this.lb_info.Text =
    "请上传图片!";
  40. }
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. this.lb_info.Text =
    "上传发生错误!原因:" + ex.ToString();
  46. }
  47. }
  48. private static
    bool IsAllowedExtension(FileUpload upfile)
  49. {
  50. string strOldFilePath =
    "";
  51. string strExtension="";
  52. string[] arrExtension ={
    ".gif", ".jpg",
    ".bmp", ".png" };
  53. if (upfile.PostedFile.FileName !=
    string.Empty)
  54. {
  55. strOldFilePath = upfile.PostedFile.FileName;//获得文件的完整路径名
  56. strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//获得文件的扩展名,如:.jpg
  57. for (int i = 0; i < arrExtension.Length; i++)
  58. {
  59. if (strExtension.Equals(arrExtension[i]))
  60. {
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. }

注意:若去掉客户端的脚本和客户端事件OnClientClick="return Check_FileType()",在后台代码

  1. //if (!IsAllowedExtension(FileUpload1))
  2. //{
  3. // this.lb_info.Text = "上传文件格式不正确!";
  4. //}
  5. if (IsAllowedExtension(FileUpload1) ==
    true)

改为:

  1. if (!IsAllowedExtension(FileUpload1))
  2. {
  3. this.lb_info.Text = "上传文件格式不正确!";
  4. }
  5. else if (IsAllowedExtension(FileUpload1) ==
    true)

即变成服务器端检查上传文件类型。

5 服务器端检查上传文件的类型(文件内部真正的格式)

  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <table>
  5. <tr>
  6. <td colspan="2">
  7. 服务器检查上传文件类型</td>
  8. </tr>
  9. <tr>
  10. <td style="width: 444px">
  11. <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
  12. <td style="width: 80px">
  13. <asp:Button ID="bt_upload" runat="server" Text="上传图片" OnClick="bt_upload_Click" /></td>
  14. </tr>
  15. <tr>
  16. <td colspan="2" style="height: 21px">
  17. <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></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. using System.IO;
  11. public partial
    class _Default : System.Web.UI.Page
  12. {
  13. protected
    void Page_Load(object sender, EventArgs e)
  14. {
  15. }
  16. protected
    void bt_upload_Click(object sender, EventArgs e)
  17. {
  18. try
  19. {
  20. if (FileUpload1.PostedFile.FileName ==
    "")
  21. {
  22. this.lb_info.Text =
    "请选择文件!";
  23. }
  24. else
  25. {
  26. string filepath = FileUpload1.PostedFile.FileName;
  27. if (IsAllowedExtension(FileUpload1) ==
    true)
  28. {
  29. string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
  30. string serverpath = Server.MapPath("images/") + filename;
  31. FileUpload1.PostedFile.SaveAs(serverpath);
  32. this.lb_info.Text =
    "上传成功!";
  33. }
  34. else
  35. {
  36. this.lb_info.Text =
    "请上传图片";
  37. }
  38. }
  39. }
  40. catch (Exception error)
  41. {
  42. this.lb_info.Text =
    "上传发生错误!原因:" + error.ToString();
  43. }
  44. }
  45. private static
    bool IsAllowedExtension(FileUpload upfile)
  46. {
  47. FileStream fs = new FileStream(upfile.PostedFile.FileName, FileMode.Open, FileAccess.Read);
  48. BinaryReader r = new BinaryReader(fs);
  49. string fileclass = "";
  50. byte buffer;
  51. try
  52. {
  53. buffer = r.ReadByte();
  54. fileclass = buffer.ToString();
  55. buffer = r.ReadByte();
  56. fileclass += buffer.ToString();
  57. }
  58. catch
  59. {
  60. }
  61. r.Close();
  62. fs.Close();
  63. if (fileclass ==
    "255216" || fileclass == "7173"||fileclass=="6677"||fileclass=="13780")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. return false;
  70. }
  71. }
  72. }

抱歉!评论已关闭.