asp.net文件上传解决方案(图片上传、单文件上传(2)

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) { if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == "") { this.lb_info.Text = "请选择文件!"; } else { HttpFileCollection myfiles = Request.Files; for (int i = 0; i < myfiles.Count; i++) { HttpPostedFile mypost = myfiles[i]; try { if (mypost.ContentLength > 0) { string filepath = mypost.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg string serverpath = Server.MapPath("~/images/") + filename;//C:\Inetpub\wwwroot\WebSite2\images\20022775_m.jpg mypost.SaveAs(serverpath); this.lb_info.Text = "上传成功!"; } } catch (Exception ex) { this.lb_info.Text = "上传发生错误!原因:" + ex.Message.ToString(); } } } } }

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 runat="server"> <div> <table> <tr> <td colspan="2"> 客户端检查上传文件类型</td> </tr> <tr> <td> <asp:FileUpload runat="server" /></td> <td> <asp:Button runat="server" Text="上传图片" OnClientClick="return Check_FileType()" /></td> </tr> <tr> <td colspan="2"> <asp:Label runat="server" ForeColor="Red"></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 = "上传文件格式不正确!"; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwpwy.html