作为自学者来说,学习编程开发真的是得认真下功夫,或许起初纯粹是看着编程好玩,深入学习发现光靠兴趣远远不够,好在互联网+的时代学习资料还是比较好找的,当然有个高手在旁指点就更好了,总是想着这个就然并卵了,还是需要我们自个儿努力。呃,话题好像扯远了,今天我们是来分享asp.net文件上传实例的,是源代码哟,而且完全可以正常运行的哟,接下来步入正题。
实例一 用Html控件HtmlInputFile
.aspx页面代码(示例中文件名为 iframeupload.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframeupload.aspx.cs" Inherits="MultiFileUpload" %>
<!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 id="Head1" runat="server">
<title>文件上传</title>
<script type="text/javascript">
function addFile() {
var div = document.createElement("div");
var f = document.createElement("input");
f.setAttribute("type", "file")
f.setAttribute("name", "File")
f.setAttribute("size", "50")
div.appendChild(f)
var d = document.createElement("input");
d.setAttribute("type", "button")
d.setAttribute("onclick", "deteFile(this)");
d.setAttribute("value", "移除")
div.appendChild(d)
document.getElementById("_container").appendChild(div);
}
function deteFile(o) {
while (o.tagName != "DIV") o = o.parentNode;
o.parentNode.removeChild(o);
}
</script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<h3>多文件上传</h3>
<div id="_container">
<input type="file" size="50" name="File"/>
</div>
<div>
<input type="button" value="添加文件(Add)" onclick="addFile()"/>
</div>
<div style="padding:10px 0">
<asp:Button runat="server" Text="开始上传" ID="UploadButton"
onclick="UploadButton_Click"></asp:Button>
</div>
<div>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</div>
</form>
</body>
</html>
.cs页面代码(示例中文件名为 iframeupload.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class MultiFileUpload : System.Web.UI.Page
{
protected void UploadButton_Click(object sender, EventArgs e)
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件分别是:<hr color='red'/>");
try
{
for (int iFile =0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName !="")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() +"<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName +"<br>");
strMsg.Append("上传文件的文件名:" + fileName +"<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension +"<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
}
实例二 使用Web控件FileUpload
.aspx页面代码(示例中文件名为 demo_UploadFile.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="demo_UploadFile.aspx.cs" Inherits="demo_UploadFile" %> <!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> </head> <body> <form id="form1" runat="server"> <div> <div> <asp:FileUpload ID="fileUpload" runat="server" /> <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传" /> <asp:Literal ID="literal" runat="server"></asp:Literal></div> </div> </form> </body> </html>
后面的带有超链接下划线的文件名,可进行预览或下载操作
.cs页面代码(示例中文件名为 demo_UploadFile.aspx.cs)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
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 demo_UploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
//判断是否上传了文件
if (fileUpload.HasFile)
{
//指定上传文件在服务器上的保存路径
string savePath = Server.MapPath("~/upload/");
//检查服务器上是否存在这个物理路径,如果不存在则创建
if (!System.IO.Directory.Exists(savePath))
{
//需要注意的是,需要对这个物理路径有足够的权限,否则会报错
//另外,这个路径应该是在网站之下,而将网站部署在C盘却把上传文件保存在D盘
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);//保存文件
//不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址
literal.Text = string.Format("<a href='../upload/{0}'>upload/{0}</a>", fileUpload.FileName);
}
}
}
说明:这两个实例均可以正常运行,需要注意的是文件之间的关联,以及在上传文件的时候,需要提前建立相应上传目录,如有不明白的,可致电postmaster@weisico.com