asp.net文件上传解决方案(图片上传、单文件上传、多文件上传、检查文件类型)
发布时间:2020-11-18 11:16:47 所属栏目:asp.Net 来源:互联网
导读:小编之前也介绍了许多ASP.NET文件上传的解决案例,今天来个asp.net文件上传大集合。
|
小编之前也介绍了许多ASP.NET文件上传的解决案例,今天来个asp.net文件上传大集合。 1 使用标准HTML来进行图片上传
<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>
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);
}
}
}
2 单文件上传
<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>
后台代码:
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 SettingsAdministratorMy DocumentsMy Pictures20022775_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:InetpubwwwrootWebSite1images20022775_m.jpg
FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
this.Upload_info.Text = "上传成功!";
}
}
catch (Exception ex)
{
this.Upload_info.Text = "上传发生错误!原因是:" + ex.ToString();
}
}
}
<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>
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 SettingsAdministratorMy DocumentsMy Pictures20022775_m.jpg
string filename = filepath.Substring(filepath.LastIndexOf("") + 1);//20022775_m.jpg
string serverpath = Server.MapPath("~/images/") + filename;//C:InetpubwwwrootWebSite2images20022775_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 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>
(编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何将两个模型传递给一个View
- 有没有办法加快asp.net中的编辑 – 编译 – 调试周期?
- 在asp.net mvc中如何使用usercontrols来显示“岛”数据?
- ASP.NET MVC3 RAZOR:文件上载给出文件计数为零
- .net – TagBuilder从MVC 3 beta版转到RC
- asp.net-mvc-3 – 用于ASP的Telerik扩展. NET MVC免费?
- 发布一款层次下拉列表控件
- iis-7.5 – 使用虚拟目录/应用程序在IIS中托管ASP.NET 5 We
- asp.net结合Ajax验证用户名是否存在的代码
- asp.net-mvc – 在Asp.Net MVC中使用千位分隔符的十进制值
推荐文章
站长推荐
- asp.net-mvc-3 – 为什么@ Html.Label()删除一些
- asp.net-mvc – 如何在我自己的自定义助手中使用
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的
- Asp.NetCore1.1版本去掉project.json后如何打包生
- asp.net 无刷新分页实例代码
- asp.net-mvc-3 – Azure网站上的RavenDb – 访问
- asp.net-mvc – AppDomain.GetAssemblies和Build
- asp.net-mvc – ASP.NET MVC如何实现返回上一页的
- asp.net – 如果我没有指定targetFramework =“4
- 从项目中的一个ASP.NET页面中删除主题
热点阅读
