使用标准HTML来进行图片上传
2011-11-09 19:59
387 查看
<body>
<form
id="form1" runat="server">
<div>
<table>
<tr>
<td
colspan="2" style="height: 21px"
>
使用标准HTML来进行图片上传</td>
</tr>
<tr>
<td
style="width: 400px">
<input
id="InputFile" style="width: 399px" type="file"
runat="server"
/></td>
<td
style="width: 80px">
<asp:Button
ID="UploadButton" runat="server" Text="上传图片"
OnClick="UploadButton_Click"
/></td>
</tr>
<tr>
<td
colspan="2"
>
<asp:Label
ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 UploadButton_Click(object sender, EventArgs e)
{
string uploadName
= InputFile.Value;//获取待上传图片的完整路径,包括文件名
//string uploadName = InputFile.PostedFile.FileName;
string pictureName
= "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
if (InputFile.Value
!= "")
{
int idx
= uploadName.LastIndexOf(".");
string suffix
= uploadName.Substring(idx);//获得上传的图片的后缀名
pictureName = DateTime.Now.Ticks.ToString()
+ suffix;
}
try
{
if (uploadName
!= "")
{
string path
= Server.MapPath("~/images/");
InputFile.PostedFile.SaveAs(path + pictureName);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
![](http://images.cnblogs.com/cnblogs_com/qiantuwuliang/202817/o_uploadfile01.gif)
2 单文件上传
这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用。其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。
前台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><body>
<form
id="form1" runat="server">
<div>
<table
style="width: 90%">
<tr>
<td
style="width: 159px" colspan=2>
<strong><span
style="font-size: 10pt">最简单的单文件上传</span></strong></td>
</tr>
<tr>
<td
style="width: 600px">
<asp:FileUpload
ID="FileUpload1" runat="server" Width="600px"
/></td>
<td
align=left>
<asp:Button
ID="FileUpload_Button" runat="server" Text="上传图片"
OnClick="FileUpload_Button_Click"
/></td>
</tr>
<tr>
<td
colspan=2>
<asp:Label
ID="Upload_info" runat="server" ForeColor="Red"
Width="767px"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 FileUpload_Button_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName
== "")
//if (FileUpload1.FileName == "")
//if (!FileUpload1.HasFile)
//获取一个值,该值指示 System.Web.UI.WebControls.FileUpload 控件是否包含文件。包含文件,则为 true;否则为 false。
{
this.Upload_info.Text
= "请选择上传文件!";
}
else
{
string filepath
= FileUpload1.PostedFile.FileName;
//得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
//string filepath = FileUpload1.FileName;
//得到上传的文件名20022775_m.jpg
string filename
= filepath.Substring(filepath.LastIndexOf("\\")
+ 1);//20022775_m.jpg
string serverpath
= Server.MapPath("~/images/")
+ filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
this.Upload_info.Text
= "上传成功!";
}
}
catch (Exception ex)
{
this.Upload_info.Text
= "上传发生错误!原因是:"
+ ex.ToString();
}
}
}
![](http://images.cnblogs.com/cnblogs_com/qiantuwuliang/202817/o_uploadfile02.gif)
3 多文件上传
前台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><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="475px"
/>
</td>
<td
style="width: 100px">
</td>
</tr>
<tr>
<td
style="width: 100px">
<asp:FileUpload
ID="FileUpload2" runat="server" Width="475px"
/></td>
<td
style="width: 100px">
</td>
</tr>
<tr>
<td
style="width: 100px">
<asp:FileUpload
ID="FileUpload3" runat="server" Width="475px"
/></td>
<td
style="width: 100px">
</td>
</tr>
<tr>
<td
style="width: 100px">
<asp:Button
ID="bt_upload" runat="server" OnClick="bt_upload_Click"
Text="一起上传"
/>
<asp:Label
ID="lb_info" runat="server" ForeColor="Red"
Width="448px"></asp:Label></td>
<td
style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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();
}
}
}
}
}
![](http://images.cnblogs.com/cnblogs_com/qiantuwuliang/202817/o_uploadfile03.gif)
4 客户端检查上传文件类型(以上传图片为例)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><%@ 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
id="form1" runat="server">
<div>
<table>
<tr>
<td
colspan="2">
客户端检查上传文件类型</td>
</tr>
<tr>
<td
style="width: 444px">
<asp:FileUpload
ID="FileUpload1" runat="server" Width="432px"
/></td>
<td
style="width: 80px">
<asp:Button
ID="bt_upload" runat="server" Text="上传图片"
OnClick="bt_upload_Click" OnClientClick="return Check_FileType()"
/></td>
</tr>
<tr>
<td
colspan="2" style="height: 21px">
<asp:Label
ID="lb_info" runat="server" ForeColor="Red"
Width="515px"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
注意:点击上传时先触发客户端事件OnClientClick="return Check_FileType()"
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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()",在后台代码
改为:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->if (!IsAllowedExtension(FileUpload1))
{
this.lb_info.Text
= "上传文件格式不正确!";
}
else if (IsAllowedExtension(FileUpload1)
== true)
即变成服务器端检查上传文件类型。
5 服务器端检查上传文件的类型(文件内部真正的格式)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><body>
<form
id="form1" runat="server">
<div>
<table>
<tr>
<td
colspan="2">
服务器检查上传文件类型</td>
</tr>
<tr>
<td
style="width: 444px">
<asp:FileUpload
ID="FileUpload1" runat="server" Width="432px"
/></td>
<td
style="width: 80px">
<asp:Button
ID="bt_upload" runat="server" Text="上传图片"
OnClick="bt_upload_Click"
/></td>
</tr>
<tr>
<td
colspan="2" style="height: 21px">
<asp:Label
ID="lb_info" runat="server" ForeColor="Red"
Width="515px"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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;
using System.IO;
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)
== 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 error)
{
this.lb_info.Text
= "上传发生错误!原因:"
+ error.ToString();
}
}
private
static bool IsAllowedExtension(FileUpload upfile)
{
FileStream fs =
new FileStream(upfile.PostedFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader r =
new BinaryReader(fs);
string fileclass
= "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
r.Close();
fs.Close();
if (fileclass
== "255216"
|| fileclass
== "7173"||fileclass=="6677"||fileclass=="13780")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return
true;
}
else
{
return
false;
}
}
}
<form
id="form1" runat="server">
<div>
<table>
<tr>
<td
colspan="2" style="height: 21px"
>
使用标准HTML来进行图片上传</td>
</tr>
<tr>
<td
style="width: 400px">
<input
id="InputFile" style="width: 399px" type="file"
runat="server"
/></td>
<td
style="width: 80px">
<asp:Button
ID="UploadButton" runat="server" Text="上传图片"
OnClick="UploadButton_Click"
/></td>
</tr>
<tr>
<td
colspan="2"
>
<asp:Label
ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 UploadButton_Click(object sender, EventArgs e)
{
string uploadName
= InputFile.Value;//获取待上传图片的完整路径,包括文件名
//string uploadName = InputFile.PostedFile.FileName;
string pictureName
= "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
if (InputFile.Value
!= "")
{
int idx
= uploadName.LastIndexOf(".");
string suffix
= uploadName.Substring(idx);//获得上传的图片的后缀名
pictureName = DateTime.Now.Ticks.ToString()
+ suffix;
}
try
{
if (uploadName
!= "")
{
string path
= Server.MapPath("~/images/");
InputFile.PostedFile.SaveAs(path + pictureName);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
![](http://images.cnblogs.com/cnblogs_com/qiantuwuliang/202817/o_uploadfile01.gif)
2 单文件上传
这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用。其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。
前台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><body>
<form
id="form1" runat="server">
<div>
<table
style="width: 90%">
<tr>
<td
style="width: 159px" colspan=2>
<strong><span
style="font-size: 10pt">最简单的单文件上传</span></strong></td>
</tr>
<tr>
<td
style="width: 600px">
<asp:FileUpload
ID="FileUpload1" runat="server" Width="600px"
/></td>
<td
align=left>
<asp:Button
ID="FileUpload_Button" runat="server" Text="上传图片"
OnClick="FileUpload_Button_Click"
/></td>
</tr>
<tr>
<td
colspan=2>
<asp:Label
ID="Upload_info" runat="server" ForeColor="Red"
Width="767px"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 FileUpload_Button_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName
== "")
//if (FileUpload1.FileName == "")
//if (!FileUpload1.HasFile)
//获取一个值,该值指示 System.Web.UI.WebControls.FileUpload 控件是否包含文件。包含文件,则为 true;否则为 false。
{
this.Upload_info.Text
= "请选择上传文件!";
}
else
{
string filepath
= FileUpload1.PostedFile.FileName;
//得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
//string filepath = FileUpload1.FileName;
//得到上传的文件名20022775_m.jpg
string filename
= filepath.Substring(filepath.LastIndexOf("\\")
+ 1);//20022775_m.jpg
string serverpath
= Server.MapPath("~/images/")
+ filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
this.Upload_info.Text
= "上传成功!";
}
}
catch (Exception ex)
{
this.Upload_info.Text
= "上传发生错误!原因是:"
+ ex.ToString();
}
}
}
![](http://images.cnblogs.com/cnblogs_com/qiantuwuliang/202817/o_uploadfile02.gif)
3 多文件上传
前台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><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="475px"
/>
</td>
<td
style="width: 100px">
</td>
</tr>
<tr>
<td
style="width: 100px">
<asp:FileUpload
ID="FileUpload2" runat="server" Width="475px"
/></td>
<td
style="width: 100px">
</td>
</tr>
<tr>
<td
style="width: 100px">
<asp:FileUpload
ID="FileUpload3" runat="server" Width="475px"
/></td>
<td
style="width: 100px">
</td>
</tr>
<tr>
<td
style="width: 100px">
<asp:Button
ID="bt_upload" runat="server" OnClick="bt_upload_Click"
Text="一起上传"
/>
<asp:Label
ID="lb_info" runat="server" ForeColor="Red"
Width="448px"></asp:Label></td>
<td
style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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();
}
}
}
}
}
![](http://images.cnblogs.com/cnblogs_com/qiantuwuliang/202817/o_uploadfile03.gif)
4 客户端检查上传文件类型(以上传图片为例)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><%@ 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
id="form1" runat="server">
<div>
<table>
<tr>
<td
colspan="2">
客户端检查上传文件类型</td>
</tr>
<tr>
<td
style="width: 444px">
<asp:FileUpload
ID="FileUpload1" runat="server" Width="432px"
/></td>
<td
style="width: 80px">
<asp:Button
ID="bt_upload" runat="server" Text="上传图片"
OnClick="bt_upload_Click" OnClientClick="return Check_FileType()"
/></td>
</tr>
<tr>
<td
colspan="2" style="height: 21px">
<asp:Label
ID="lb_info" runat="server" ForeColor="Red"
Width="515px"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
注意:点击上传时先触发客户端事件OnClientClick="return Check_FileType()"
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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()",在后台代码
改为:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->if (!IsAllowedExtension(FileUpload1))
{
this.lb_info.Text
= "上传文件格式不正确!";
}
else if (IsAllowedExtension(FileUpload1)
== true)
即变成服务器端检查上传文件类型。
5 服务器端检查上传文件的类型(文件内部真正的格式)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><body>
<form
id="form1" runat="server">
<div>
<table>
<tr>
<td
colspan="2">
服务器检查上传文件类型</td>
</tr>
<tr>
<td
style="width: 444px">
<asp:FileUpload
ID="FileUpload1" runat="server" Width="432px"
/></td>
<td
style="width: 80px">
<asp:Button
ID="bt_upload" runat="server" Text="上传图片"
OnClick="bt_upload_Click"
/></td>
</tr>
<tr>
<td
colspan="2" style="height: 21px">
<asp:Label
ID="lb_info" runat="server" ForeColor="Red"
Width="515px"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
后台代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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;
using System.IO;
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)
== 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 error)
{
this.lb_info.Text
= "上传发生错误!原因:"
+ error.ToString();
}
}
private
static bool IsAllowedExtension(FileUpload upfile)
{
FileStream fs =
new FileStream(upfile.PostedFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader r =
new BinaryReader(fs);
string fileclass
= "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
r.Close();
fs.Close();
if (fileclass
== "255216"
|| fileclass
== "7173"||fileclass=="6677"||fileclass=="13780")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return
true;
}
else
{
return
false;
}
}
}
相关文章推荐
- html使用canvas进行图片压缩上传 (废话不说直接贴代码,不懂的话在讨论)
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
- 使用Netty进行Android与Server端通信实现文字发送接收与图片上传
- Android开发中使用七牛云存储进行图片上传下载
- OC - 使用AFNetworking进行图片上传
- 前台使用ajax上传图片,后台 node.js 使用 formidable 进行图片接受并存于服务器静态文件夹中
- C# 使用FileUpload控件上传图片,将文件转换成二进制进行存储与读取
- node.js(express)中使用Jcrop进行图片裁切上传
- 商城项目实战20:使用FastDFS-Client客户端进行上传图片的测试
- 使用ueditor进行图片上传、保存、存入数据库
- SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
- 使用Activex插件复制剪贴板中的word或excel内容到web HTML编辑器并自动上传图片或文件
- 淘淘商城系列——使用FastDFS-Client客户端进行上传图片的测试
- vue之使用Cropper进行图片剪裁上传压缩
- SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
- JSP+Servlet中使用jspsmartupload.jar进行图片上传下载
- html的img中使用SVG图片做SRC在本地测试可用但是上传服务器后不能显示问题解决 标签: SVGMIME TYPE不显示服务器设置 2016-05-21 13:49 2517人阅读 评论(0)
- 使用Netty进行Android与Server端通信实现文字发送接收与图片上传
- 使用base64进行移动端图片上传