ASP.NET2.0文件上传以及图片处理总结篇 [转]
发布时间:2020-12-15 04:40:14 所属栏目:百科 来源:网络整理
导读:p align="left" font size="2"1.最简单的单文件上传(没花头) 2.多文件上传 3.客户端检查上传文件类型(以上传图片为例) font size="2"4.服务器端检查上传文件类型(以上传图片为例) font size="2"5.服务器端检查上传文件类型(可以检测真正文件名) font size="2
<p align="left"><font size="2">1.最简单的单文件上传(没花头) <p align="left"><font size="2">1.最简单的单文件上传(没花头) <p align="left"><font size="2">效果图:<a href="http://tmsoft.lsxy.com/attachments/200708/0551756948.gif" target="_blank" rel="lightbox[plants]"> <img height="212" alt="" src="http://tmsoft.lsxy.com/attachments/saveimages/o_f1.gif" width="547"> <font size="2">说明:这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用。其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。<p align="left"><font size="2">后台代码: using System; using System.Data; using System.Configuration; using System.Collections; 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; <p align="left"><font size="2">public partial class _Default : System.Web.UI.Page { ??? protected void Page_Load(object sender,EventArgs e) ??? { <font size="2">??? } ??? 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("images/") + filename; ??????????????? FileUpload1.PostedFile.SaveAs(serverpath); ??????????????? this.lb_info.Text = "上传成功!"; ??????????? } ??????? } ??????? catch (Exception ex) ??????? { ??????????? this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString(); ??????? } ??? } } 前台代码: <table style="width: 343px"> ??????????? <tr> ??????????????? <td style="width: 100px"> ??????????????????? 单文件上传</td> ??????????????? <td style="width: 100px"> ??????????????? </td> ??????????? </tr> ??????????? <tr> ??????????????? <td style="width: 100px"> ??????????????????? ??????????????????? </td> ??????????????? <td style="width: 100px"> ??????????????????? </td> ??????????? </tr> ??????????? <tr> ??????????????? <td style="width: 100px; height: 21px;"> ??????????????????? <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label></td> ??????????????? <td style="width: 100px; height: 21px"> ??????????????? </td> ??????????? </tr> ??????? </table>? <p align="left"><font size="2">2.多文件上传 <p align="left"><font size="2">效果图: <img height="212" alt="" src="http://tmsoft.lsxy.com/attachments/saveimages/o_f2.gif" width="547"> <p align="left"><font size="2">后台代码:using System; using System.Data; using System.Configuration; using System.Collections; 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; <p align="left"><font size="2">public partial class _Default : System.Web.UI.Page { ??? protected void Page_Load(object sender,EventArgs e) ??? { <p align="left"><font size="2">??? } ??? 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; ???????????????????????????? string filename = filepath.Substring(filepath.LastIndexOf("") + 1); ???????????????????????????? string serverpath = Server.MapPath("images/") + filename; ???????????????????????????? mypost.SaveAs(serverpath); ???????????????????????????? this.lb_info.Text = "上传成功!"; ??????????????????????? } ??????????????????? } ??????????????????? catch (Exception error) ??????????????????? { ??????????????????????? this.lb_info.Text = "上传发生错误!原因:" + error.ToString(); ??????????????????? } <p align="left"><font size="2">??????????????? } ?????????????? ??????????? } ??????? } ?????? ??? }前台代码:?<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <p align="left"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><font color="#000080">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <p align="left"><html xmlns="<a href="http://www.w3.org/1999/xhtml"><font color="#000080">http://www.w3.org/1999/xhtml" > <head runat="server"> ??? |