1、使用FileUpload控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Study_FileUpload1.aspx.cs" Inherits="StudyFromNow.Study_FileUpload1" %>
<!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>上传-FileUpload</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<fieldset style="width: 290px">
<legend>FileUpload控件</legend>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="BtnUpload" runat="server" Text="上传" OnClick="BtnUpload_Click" />
<hr />
<asp:Label ID="lblMsg" runat="server" ForeColor="red" /><br />
<asp:Label ID="lblMsg2" runat="server" />
</fieldset>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace StudyFromNow
{
public partial class Study_FileUpload1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnUpload_Click(object sender, EventArgs e)
{
bool isAllowed = false;
string destPath = Server.MapPath("~/Temp/");
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
isAllowed = true;
}
}
}
if (isAllowed)
{
try
{
FileUpload1.SaveAs(destPath + FileUpload1.FileName);
lblMsg.Text = "文件上传成功.";
lblMsg2.Text = "<b>原文件路径:</b>" + FileUpload1.PostedFile.FileName + "<br />" +
"<b>文件大小:</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" +
"<b>文件类型:</b>" + FileUpload1.PostedFile.ContentType + "<br />";
}
catch (Exception ex)
{
lblMsg.Text = "文件上传不成功.";
}
}
else
{
lblMsg.Text = "只能够上传图片文件.";
}
}
}
}
2、使用 input type="file"
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Study_FileUpload2.aspx.cs"
Inherits="StudyFromNow.Study_FileUpload2" %>
<!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>文件上传-INPUT</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>文件上传-INPUT</legend>
<input type="file" id="UploadFile" name="UploadFile" runat="server" />
<br />
<asp:Button ID="btn_upload" runat="server" Text="上传" OnClick="btn_upload_Click" />
<hr />
<asp:Label ID="lblMsg" runat="server" ForeColor="red" /><br />
<asp:Label ID="lblMsg2" runat="server" />
</fieldset>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace StudyFromNow
{
public partial class Study_FileUpload2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_upload_Click(object sender, EventArgs e)
{
if (UploadFile.PostedFile.FileName.Trim() != "")
{
bool isAllowed = false;
isAllowed = fileChecked(UploadFile);
string destPath = Server.MapPath("~/Temp/");
string fileName = UploadFile.PostedFile.FileName;
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
if (isAllowed)
{
try
{
UploadFile.PostedFile.SaveAs(destPath + fileName);
lblMsg.Text = "文件上传成功.";
lblMsg2.Text = "<b>原文件路径:</b>" + UploadFile.PostedFile.FileName + "<br />" +
"<b>文件大小:</b>" + UploadFile.PostedFile.ContentLength + "字节<br />" +
"<b>文件类型:</b>" + UploadFile.PostedFile.ContentType + "<br />";
}
catch (Exception ex)
{
lblMsg.Text = "文件上传不成功.";
}
}
else
{
lblMsg.Text = "只能够上传图片文件.";
}
}
}
private bool fileChecked(HtmlInputFile UploadFile)
{
bool isAllowed = false;
string fileExtension = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName.Trim());
string[] allowedExtenstions = { ".gif", ".png", ".bmp", ".jpg" };
for (int i = 0; i < allowedExtenstions.Length; i++)
{
if (fileExtension == allowedExtenstions[i])
{
isAllowed = true;
}
else
{
isAllowed = false;
}
}
return isAllowed;
}
}
}
注:两种方法基本相同,只是cs文件对于上传文件处理略有不同,作为记录,方便查阅。
分享到:
相关推荐
在ASP.NET中实现多文件上传
在ASP.NET中实现多文件上传的简单代码实现,可供参考。
基于jquery.form插件写的ASP.NET文件批量上传。
一个简单的ASP.NET实现多文件上传代码
FileUpload 文件上传组件的ASP.NET实例,基于主副窗口的上传程序,利用线程提升上传速度。本插件可支持多文件上传,为了提升上传体验,这里使用了AjaxControlToolkit.dll、... ASP.NET中实现FileUpload多文件上传
ASP.NET WebAPI 接口 实现单文件,多文件上传下载,图片缩略图访问实现接口,实用于APP,web系统应用,上传下载文件、图片访问分离处理。
WEBUPLOAD大文件传送C# asp.netWEBUPLOAD大文件传送C# asp.netWEBUPLOAD大文件传送C# asp.net
asp.net实现单文件上传,源代码,可实现单一的文件上传
需求背景:以前在做文件上传下载,都是基于FrameWork项目或者Core Web项目实现的文件上传或下载,基于web的文件下载及权限比较好实现的。但是现在很多公司技术领导层,都在推行前后端分离理念,所以这一次我承担了...
ASP.NET Web API File Upload and Multipart MIME
这是一个通过asp.net利用mvc思想,实现的一个文件上传的例子,很适合初学者学习
asp.net操作文件的上传、读出、删除等操作!!!
asp.net上传 多文件上传 批量上传 上传代码 上载asp.net上传 多文件上传 批量上传 上传代码 上载asp.net上传 多文件上传 批量上传 上传代码 上载asp.net上传 多文件上传 批量上传 上传代码 上载asp.net上传 多文件...
图片文件上传代码 图片文件上传代码 图片文件上传代码 图片文件上传代码
Asp.NET 文件上传下载 Asp.NET 文件上传下载 Asp.NET 文件上传下载 Asp.NET 文件上传下载
asp.net+ajax 实现的多文件上传, 支持文件单独和批量上传,单个和批量移除
【ASP.NET编程知识】asp.net利用ashx文件实现文件的上传功能.docx
如果服务器上安装了RAR程序,那么asp.net可以调用RAR实现文件压缩与解压缩。 不过要注意的是,由于Web程序不能直接调用客户端的程序(除非用ActiveX,ActiveX几乎被废弃),所以如果要想实现让用户把本地文件用网页...