ASP. NET to Upload Large Files
- 2021-08-12 02:25:41
- OfStack
Need to download NeatUpload plug-in
Upload page:
<%@ 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"> Upper Biography </asp:LinkButton><br /><br />
<a href="Down.aspx"> View Download </a>
</div>
</form>
</body>
</html>
Background source code:
protected void BtnUP_Click(object sender, EventArgs e)
{
if (AttachFile.HasFile)
{
string FileName = this.AttachFile.FileName;// Get the file name of the uploaded file , Include suffix
string ExtenName = System.IO.Path.GetExtension(FileName);// Get Extensions
string SaveFileName = System.IO.Path.Combine(System.Web.HttpContext.Current.Request.MapPath("UpLoads/"), DateTime.Now.ToLongDateString().ToString() + "-" + FileName);// Merge the two paths into the full path uploaded to the server
AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
string url = "UpLoads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ExtenName; // Path to save the file
float FileSize = (float)System.Math.Round((float)AttachFile.ContentLength / 1024000, 1); // Get the file size and keep the decimal point 1 Bit , Unit is M
Response.Write("<script>alert(' Congratulations, upload successfully !')</script>");
}
else
{
Response.Write("<script>alert(' Please select a file! ')</script>");
}
}
Here, get the file name of the upload folder directly and provide download
<%@ 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"> Upload a file </a>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<ul id="www_zzjs_net">
<%if (files.Length == 0) { Label1.Text = " Documents are not available at present "; }
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(' Are you sure you want to delete it? ');" > Delete attachments </a>
</li>
<% }
}%>
</ul>
</div>
<div id="changpage"></div>
<%--js Paging --%>
<%--<script language="javascript">
var obj, j;
var page = 0;
var nowPage = 0; // Current page
var listNum = 25; // Display per page <ul> Number
var PagesLen; // Total pages
var PageNum = 4; // Number of paging links (5 A )
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
// Content transformation
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"
} // Welcome to the webmaster special effects network, our website is www.zzjs.net , it's easy to remember, zz Stationmaster, js Is js Special effects, this site collects a large number of high quality js Code, and many advertising codes to download.
// Paging link transformation
strS = '<a href="###" onclick="upPage(0)"> Home page </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 } // Home page
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> '
} // Welcome to the webmaster special effects network, our website is www.zzjs.net , it's easy to remember, zz Stationmaster, js Is js Special effects, this site collects a large number of high quality js Code, and many advertising codes to download.
strE = ' <a href="###" onclick="upPage(' + (PagesLen - 1) + ')"> End page </a> '
strE2 = nowPage + 1 + "/" + PagesLen + " Page " + " Altogether " + j + " Article "
document.getElementById("changpage").innerHTML = strS + strC + strE + strE2
}
</script>--%>
</form>
</body>
</html>
// Here's the background code
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 = " Documents are not available at present "; }
}
// This is a new another 1 Pages provide downloaded code
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();
}
}
// This is a new another 1 Pages provide deletion (background)
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(' Delete successful! ');window.location = 'Down.aspx';</script>");
//Response.Redirect("Down.aspx");
}
The above is the whole content of this paper, hoping to help everyone's study.