检查文件类型
新建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;}} }后臺,也是通過調用一個方法來判斷文件類型
在寫業務邏輯代碼的時候,不要把所有的業務邏輯都寫到一個方法中,盡量寫在多個獨立的方法中,通過調用來執行,這樣,業務邏輯清楚,代碼也會更加整潔
總結
- 上一篇: ASP.NET实现文件上传
- 下一篇: 保证文件名的唯一性