新建ASP.NET Web应用程序
客户端检查文件类型
效果
页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebDropzone.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 343px">
<tr>
<td style="width: 100px">客户端检查文件类型</td>
<td style="width: 100px"></td>
</tr>
<tr>
<td style="width: 100px">
<asp:FileUpload ID="FileUpload1" runat="server" Width="400px" />
</td>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="bt_upload_Click" Text="上传" OnClientClick="return Check_FileType()" />
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label></td>
<td style="width: 100px"></td>
</tr>
</table>
</div>
</form>
</body>
<script>
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>
</html>
就是,通过调用一个js方法来判断文件类型
后台代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebDropzone
{
public partial class WebForm : System.Web.UI.Page, IHttpHandler
{
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;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string serverpath = Server.MapPath("File/") + filename;
FileUpload1.PostedFile.SaveAs(serverpath);
this.lb_info.Text = "上传成功!";
}
}
catch (Exception ex)
{
this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString();
}
}
}
}
服务端检查文件类型
效果
页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebDropzone.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 343px">
<tr>
<td style="width: 100px">服务端检查文件类型</td>
<td style="width: 100px"></td>
</tr>
<tr>
<td style="width: 100px">
<asp:FileUpload ID="FileUpload1" runat="server" Width="400px" />
</td>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="bt_upload_Click" Text="上传"/>
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label></td>
<td style="width: 100px"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
namespace WebDropzone
{
public partial class WebForm : System.Web.UI.Page, IHttpHandler
{
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("File/") + filename;
FileUpload1.PostedFile.SaveAs(serverpath);
this.lb_info.Text = "上传成功!";
}
else
{
this.lb_info.Text = "请上传图片";
}
}
}
catch (Exception ex)
{
this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString();
}
}
// 检查上传文件类型
public static bool IsAllowedExtension(FileUpload hifile)
{
string strOldFilePath = "", strExtension = "";
string[] arrExtension = { ".gif", ".jpg", ".jpeg", ".bmp", ".png" };
if (hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
for (int i = 0; i < arrExtension.Length; i++)
{
if (strExtension.Equals(arrExtension[i]))
{
return true;
}
}
}
return false;
}
}
}
后台,也是通过调用一个方法来判断文件类型
在写业务逻辑代码的时候,不要把所有的业务逻辑都写到一个方法中,尽量写在多个独立的方法中,通过调用来执行,这样,业务逻辑清楚,代码也会更加整洁