日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

检查文件类型

發布時間:2025/3/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 检查文件类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

新建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;}} }

后臺,也是通過調用一個方法來判斷文件類型

在寫業務邏輯代碼的時候,不要把所有的業務邏輯都寫到一個方法中,盡量寫在多個獨立的方法中,通過調用來執行,這樣,業務邏輯清楚,代碼也會更加整潔

總結

以上是生活随笔為你收集整理的检查文件类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。