ASP.NET實現(xiàn)大文件上傳功能
需要下載NeatUpload插件
上傳頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Brettle.Web.NeatUpload" Namespace="Brettle.Web.NeatUpload"
TagPrefix="Upload" %>
<!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>
<link href="easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<link href="easyui/themes/icon.css" rel="stylesheet" type="text/css" />
<script src="easyui/jquery.min.js" type="text/javascript"></script>
<script src="easyui/jquery.easyui.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="easyui-panel" style="padding:5px;">
<Upload:ProgressBar ID="ProgressBar1" runat='server'>
</Upload:ProgressBar>
<br />
<Upload:InputFile ID="AttachFile" runat="server" />
<br />
<br />
<asp:LinkButton ID="BtnUP" runat="server" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="BtnUP_Click">上 傳</asp:LinkButton><br /><br />
<a href="Down.aspx">查看下載</a>
</div>
</form>
</body>
</html>
后臺源碼:
protected void BtnUP_Click(object sender, EventArgs e)
{
if (AttachFile.HasFile)
{
string FileName = this.AttachFile.FileName;//獲取上傳文件的文件名,包括后綴
string ExtenName = System.IO.Path.GetExtension(FileName);//獲取擴(kuò)展名
string SaveFileName = System.IO.Path.Combine(System.Web.HttpContext.Current.Request.MapPath("UpLoads/"), DateTime.Now.ToLongDateString().ToString() + "-" + FileName);//合并兩個路徑為上傳到服務(wù)器上的全路徑
AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
string url = "UpLoads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ExtenName; //文件保存的路徑
float FileSize = (float)System.Math.Round((float)AttachFile.ContentLength / 1024000, 1); //獲取文件大小并保留小數(shù)點后一位,單位是M
Response.Write("<script>alert('恭喜您,上傳成功!')</script>");
}
else
{
Response.Write("<script>alert('請選擇文件!')</script>");
}
}
這里直接獲取上傳文件夾文件名稱提供下載
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Down.aspx.cs" Inherits="Down" %>
<!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>
<link href="easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<link href="easyui/themes/icon.css" rel="stylesheet" type="text/css" />
<script src="easyui/jquery.min.js" type="text/javascript"></script>
<script src="easyui/jquery.easyui.min.js" type="text/javascript"></script>
<style type="text/css">
.div_style ul li
{
list-style-type: decimal;
border-bottom:1px dashed #E3E3E3;
margin-bottom:5px;
}
.div_style ul li a
{
text-decoration: none;
cursor: pointer;
font-size: 16px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="div_style easyui-panel" style="padding: 5px;">
<a href="index.aspx">上傳文件</a>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<ul id="www_zzjs_net">
<%if (files.Length == 0) { Label1.Text = "暫無文件"; }
else
{
foreach (System.IO.FileInfo item in files)
{%>
<li><a href="Default2.aspx?1=<%=item%>">
<%=item %></a> <a href="delete.aspx?1=<%=item%>" onclick="javascript:return confirm('確定刪除嗎?');" >刪除附件</a>
</li>
<% }
}%>
</ul>
</div>
<div id="changpage"></div>
<%--js分頁--%>
<%--<script language="javascript">
var obj, j;
var page = 0;
var nowPage = 0; //當(dāng)前頁
var listNum = 25; //每頁顯示<ul>數(shù)
var PagesLen; //總頁數(shù)
var PageNum = 4; //分頁鏈接接數(shù)(5個)
onload = function () {
obj = document.getElementById("www_zzjs_net").getElementsByTagName("li");
j = obj.length
PagesLen = Math.ceil(j / listNum);
upPage(0)
}
function upPage(p) {
nowPage = p
//內(nèi)容變換
for (var i = 0; i < j; i++) {
obj[i].style.display = "none"
}
for (var i = p * listNum; i < (p + 1) * listNum; i++) {
if (obj[i]) obj[i].style.display = "block"
} //歡迎來到站長特效網(wǎng),我們的網(wǎng)址是www.zzjs.net,很好記,zz站長,js就是js特效,本站收集大量高質(zhì)量js代碼,還有許多廣告代碼下載。
//分頁鏈接變換
strS = '<a href="###" onclick="upPage(0)">首頁</a> '
var PageNum_2 = PageNum % 2 == 0 ? Math.ceil(PageNum / 2) + 1 : Math.ceil(PageNum / 2)
var PageNum_3 = PageNum % 2 == 0 ? Math.ceil(PageNum / 2) : Math.ceil(PageNum / 2) + 1
var strC = "", startPage, endPage;
if (PageNum >= PagesLen) { startPage = 0; endPage = PagesLen - 1 }
else if (nowPage < PageNum_2) { startPage = 0; endPage = PagesLen - 1 > PageNum ? PageNum : PagesLen - 1 } //首頁
else { startPage = nowPage + PageNum_3 >= PagesLen ? PagesLen - PageNum - 1 : nowPage - PageNum_2 + 1; var t = startPage + PageNum; endPage = t > PagesLen ? PagesLen - 1 : t }
for (var i = startPage; i <= endPage; i++) {
if (i == nowPage) strC += '<a href="###" style="color:red;font-weight:700;" onclick="upPage(' + i + ')">' + (i + 1) + '</a> '
else strC += '<a href="###" onclick="upPage(' + i + ')">' + (i + 1) + '</a> '
} //歡迎來到站長特效網(wǎng),我們的網(wǎng)址是www.zzjs.net,很好記,zz站長,js就是js特效,本站收集大量高質(zhì)量js代碼,還有許多廣告代碼下載。
strE = ' <a href="###" onclick="upPage(' + (PagesLen - 1) + ')">尾頁</a> '
strE2 = nowPage + 1 + "/" + PagesLen + "頁" + " 共" + j + "條"
document.getElementById("changpage").innerHTML = strS + strC + strE + strE2
}
</script>--%>
</form>
</body>
</html>
//下面是后臺代碼
public FileInfo[] files { set; get; }
protected void Page_Load(object sender, EventArgs e)
{
string dirPath = HttpContext.Current.Server.MapPath("~/UpLoads");
if (Directory.Exists(dirPath))
{
DirectoryInfo dir = new DirectoryInfo(dirPath);
files = dir.GetFiles("*.*");
}
if (files.Length == 0) { Label1.Text = "暫無文件"; }
}
//這是新建另一個頁面提供下載的代碼
if (Request.QueryString["1"] != null)
{
string _filename = Request.QueryString["1"].ToString(), _filepath = System.IO.Path.Combine(MapPath("~/UpLoads"), _filename);
if (System.IO.File.Exists(_filepath))
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment;filename=" + _filename);
Response.ContentType = "application/unknow";
Response.TransmitFile(_filepath);
Response.End();
}
}
//這是新建另一個頁面提供刪除操作(后臺)
string _filename = Request.QueryString["1"].ToString(), _filepath = System.IO.Path.Combine(MapPath("~/UpLoads"), _filename);
if (System.IO.File.Exists(_filepath))
{
File.Delete(_filepath);
Response.Write("<script language=javascript>alert('刪除成功!');window.location = 'Down.aspx';</script>");
//Response.Redirect("Down.aspx");
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
asp.net 需要登陸的網(wǎng)站上下載網(wǎng)頁源代碼和文件
最近有個項目需要從網(wǎng)絡(luò)上下載網(wǎng)頁信息和文件,并且需要登錄后才能下載,所以做了個下載的通用類,供大家參考。2009-05-05
asp.net中Post表單保存頁面狀態(tài)并輸出源碼的實現(xiàn)方法
先執(zhí)行腳本,復(fù)制源碼到隱藏域里,再輸出源碼,注意代碼紅色設(shè)置2012-08-08
Asp.net MVC中Razor常見的問題與解決方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Asp.net MVC中Razor常見的問題與解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
asp.net Parameters.AddWithValue方法在SQL語句的 Where 字句中的用法
今天晚上看論壇,有人提問說,Parameters.AddWithValue方法在有些情況下不好使2009-01-01
asp.net GridView控件中模板列CheckBox全選、反選、取消
GridView控件中模板列CheckBox全選、反選、取消實現(xiàn)代碼。2009-12-12
關(guān)于EF的Code?First的使用以及踩坑記錄
這篇文章主要介紹了關(guān)于EF的Code?First的使用以及踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Visual Studio 2017 community安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2017 community安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09
ASP.NET抓取網(wǎng)頁內(nèi)容的實現(xiàn)方法
這篇文章主要介紹了ASP.NET抓取網(wǎng)頁內(nèi)容的實現(xiàn)方法,涉及使用HttpWebRequest及WebResponse抓取網(wǎng)頁內(nèi)容的技巧,需要的朋友可以參考下2015-02-02
asp.net Javascript獲取CheckBoxList的value
最近在做一個BS的小項目,記得自己搞asp.net的時候,還是兩年以前,大部分的東西只是有點印象,忘得差不多了,所以這次也算是溫習(xí)的過程吧,一邊學(xué)習(xí),一邊趕工,呵呵呵。。。。2009-12-12
C# 生成驗證碼取隨機(jī)數(shù)字加字母(改進(jìn)版)
生成驗證碼取4位數(shù)字沒有問題,結(jié)果加上字母效率非常低本人改進(jìn)了一下,感興趣的朋友可以參考下哈2013-03-03

