4 客户端检查上传文件类型(以上传图片为例)
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%> - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> - <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>客户端检查上传文件类型</title>
- <script language="javascript">
- function Check_FileType()
- {
- var str=document.getElementById("FileUpload1").value;
- var pos=str.lastIndexOf(".");
- var lastname=str.substring(pos,str.length);
- if(lastname.toLowerCase()!=".jpg"&&lastname.toLowerCase()!=".gif")
- {
- alert("您上传的文件类型为"+lastname+",图片必须为.jpg,.gif类型");
- return false;
- }
- else
- {
- return true;
- }
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td colspan="2">
- 客户端检查上传文件类型</td>
- </tr>
- <tr>
- <td style="width: 444px">
- <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
- <td style="width: 80px">
- <asp:Button ID="bt_upload" runat="server" Text="上传图片" OnClick="bt_upload_Click"
OnClientClick="return Check_FileType()" /></td> - </tr>
- <tr>
- <td colspan="2" style="height: 21px">
- <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
注意:点击上传时先触发客户端事件OnClientClick="return Check_FileType()"
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected
void bt_upload_Click(object sender, EventArgs e) - {
- try
- {
- if (FileUpload1.PostedFile.FileName ==
"") - {
- this.lb_info.Text =
"请选择文件!"; - }
- else
- {
- string filepath = FileUpload1.PostedFile.FileName;
- //if (!IsAllowedExtension(FileUpload1))
- //{
- // this.lb_info.Text = "上传文件格式不正确!";
- //}
- if (IsAllowedExtension(FileUpload1) ==
true) - {
- string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
- string serverpath = Server.MapPath("~/images/") + filename;
- FileUpload1.PostedFile.SaveAs(serverpath);
- this.lb_info.Text =
"上传成功!"; - }
- else
- {
- this.lb_info.Text =
"请上传图片!"; - }
- }
- }
- catch (Exception ex)
- {
- this.lb_info.Text =
"上传发生错误!原因:" + ex.ToString(); - }
- }
- private static
bool IsAllowedExtension(FileUpload upfile) - {
- string strOldFilePath =
""; - string strExtension="";
- string[] arrExtension ={
".gif", ".jpg",
".bmp", ".png" }; - if (upfile.PostedFile.FileName !=
string.Empty) - {
- strOldFilePath = upfile.PostedFile.FileName;//获得文件的完整路径名
- strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//获得文件的扩展名,如:.jpg
- for (int i = 0; i < arrExtension.Length; i++)
- {
- if (strExtension.Equals(arrExtension[i]))
- {
- return true;
- }
- }
- }
- return false;
- }
- }
注意:若去掉客户端的脚本和客户端事件OnClientClick="return Check_FileType()",在后台代码
- //if (!IsAllowedExtension(FileUpload1))
- //{
- // this.lb_info.Text = "上传文件格式不正确!";
- //}
- if (IsAllowedExtension(FileUpload1) ==
true)
改为:
- if (!IsAllowedExtension(FileUpload1))
- {
- this.lb_info.Text = "上传文件格式不正确!";
- }
- else if (IsAllowedExtension(FileUpload1) ==
true)
即变成服务器端检查上传文件类型。
5 服务器端检查上传文件的类型(文件内部真正的格式)
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td colspan="2">
- 服务器检查上传文件类型</td>
- </tr>
- <tr>
- <td style="width: 444px">
- <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td>
- <td style="width: 80px">
- <asp:Button ID="bt_upload" runat="server" Text="上传图片" OnClick="bt_upload_Click" /></td>
- </tr>
- <tr>
- <td colspan="2" style="height: 21px">
- <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>
- </tr>
- </table>
- </div>
- </form>
- </body>
后台代码:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.IO;
- public partial
class _Default : System.Web.UI.Page - {
- protected
void Page_Load(object sender, EventArgs e) - {
- }
- protected
void bt_upload_Click(object sender, EventArgs e) - {
- try
- {
- if (FileUpload1.PostedFile.FileName ==
"") - {
- this.lb_info.Text =
"请选择文件!"; - }
- else
- {
- string filepath = FileUpload1.PostedFile.FileName;
- if (IsAllowedExtension(FileUpload1) ==
true) - {
- string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
- string serverpath = Server.MapPath("images/") + filename;
- FileUpload1.PostedFile.SaveAs(serverpath);
- this.lb_info.Text =
"上传成功!"; - }
- else
- {
- this.lb_info.Text =
"请上传图片"; - }
- }
- }
- catch (Exception error)
- {
- this.lb_info.Text =
"上传发生错误!原因:" + error.ToString(); - }
- }
- private static
bool IsAllowedExtension(FileUpload upfile) - {
- FileStream fs = new FileStream(upfile.PostedFile.FileName, FileMode.Open, FileAccess.Read);
- BinaryReader r = new BinaryReader(fs);
- string fileclass = "";
- byte buffer;
- try
- {
- buffer = r.ReadByte();
- fileclass = buffer.ToString();
- buffer = r.ReadByte();
- fileclass += buffer.ToString();
- }
- catch
- {
- }
- r.Close();
- fs.Close();
- if (fileclass ==
"255216" || fileclass == "7173"||fileclass=="6677"||fileclass=="13780")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }