擁有網(wǎng)頁(yè)版小U盤(pán) ASP.NET實(shí)現(xiàn)文件上傳與下載功能
今天看到了一篇不錯(cuò)的文章,就拿來(lái)一起分享一下吧。
實(shí)現(xiàn)的是文件的上傳與下載功能。
關(guān)于文件上傳:
談及文件上傳到網(wǎng)站上,首先我們想到的就是通過(guò)什么上傳呢?在ASP.NET中,只需要用FileUpload控件即可完成,但是默認(rèn)上傳4M大小的數(shù)據(jù),當(dāng)然了你可以在web.config文件中進(jìn)行修改,方式如下:
<system.web>
<httpRuntime executionTimeout="240"
maxRequestLength="20480"/>
</system.web>
但是這種方式雖然可以自定義文件的大小,但并不是無(wú)極限的修改的
下一步,現(xiàn)在“工具”有了,要怎么上傳呢?按照直覺(jué)是不是應(yīng)該先選中我想要上傳的文件呢?這就對(duì)了,因?yàn)閺腇ileUpload控件返回后我們便已經(jīng)得到了在客戶端選中的文件的信息了,接下來(lái)就是將這個(gè)文件進(jìn)行修改(具體的操作是:去掉所得路徑下的盤(pán)符的信息,換成服務(wù)器上的相關(guān)路徑下,不過(guò)這里并沒(méi)有更改原本文件的名稱)。然后調(diào)用相關(guān)的上傳方法就好了。
先看一下界面文件吧
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<br />
<br />
<br />
<br />
<asp:ImageButton ID="ImageButton_Up" runat="server" OnClick="ImageButton_Up_Click" style="text-decoration: underline" ToolTip="Up" Width="54px" />
<asp:ImageButton ID="ImageButton_Down" runat="server" OnClick="ImageButton_Down_Click" ToolTip="Download" Width="51px" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
然后是具體的邏輯
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
? protected void Page_Load(object sender, EventArgs e)
? {
? }
? //a method for currying file updown
? private void UpFile()
? {
? ? String strFileName;
? ? //get the path of the file
? ? String FilePath = Server.MapPath("./") + "File";
? ? //judge weather has file to upload
? ? if (FileUpload1.PostedFile.FileName != null)
? ? {
? ? ? strFileName = FileUpload1.PostedFile.FileName;
? ? ? //save all the message of the file
? ? ? strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
? ? ? try
? ? ? {
? ? ? ? FileUpload1.SaveAs(FilePath + "\\" + this.FileUpload1.FileName);
? ? ? ? //save the file and obey the rules
? ? ? ? Label1.Text = "Upload success!";
? ? ? }
? ? ? catch (Exception e)
? ? ? {
? ? ? ? Label1.Text = "Upload Failed!"+e.Message.ToString();
? ? ? }
? ? }
? }
? protected void ImageButton_Up_Click(object sender, ImageClickEventArgs e)
? {
? ? UpFile();
? }
? protected void ImageButton_Down_Click(object sender, ImageClickEventArgs e)
? {
? ? Response.Redirect("DownFile.aspx");
? }
}說(shuō)完了上傳,下面談一談文件的下載。這里主要是借助于Directory對(duì)象的GetFiles()方法,其可以獲得指定路徑下的所有的文件的名稱。這樣我們就可以用之來(lái)填充一個(gè)listBox,來(lái)供我們選擇到底要下載那一個(gè)文件。
也許這時(shí)你會(huì)有一點(diǎn)疑惑了,我現(xiàn)在知道了有哪些文件可以下載,那下一步我要怎么來(lái)實(shí)現(xiàn)呢?
其實(shí)這里是利用了Session的存儲(chǔ)機(jī)制,那就是將我們?cè)趌istbox 中選擇的item的內(nèi)容記錄到session的特定的key中,這樣的話,我們就可以不用關(guān)心這些信息在頁(yè)面間是怎么傳輸?shù)牧?。只需要在想要進(jìn)行下載的地方直接獲取就可以了。
最為核心的是下載的過(guò)程:
if (filepathinfo.Exists)
{
//save the file to local
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filepathinfo.Name));
Response.AddHeader("Content-length", filepathinfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(filepathinfo.FullName);
Response.End();
}
下面看一下,下載界面的布局文件吧
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DownFile.aspx.cs" Inherits="DownFile" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ImageButton ID="ImageButton_Up" runat="server" Height="56px" OnClick="ImageButton_Up_Click" ToolTip="Upload" Width="90px" />
<asp:ImageButton ID="ImageButton_Down" runat="server" Height="52px" OnClick="ImageButton_Down_Click" style="margin-top: 0px" ToolTip="Download" Width="107px" />
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:ListBox ID="ListBox1" runat="server" Height="169px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="371px"></asp:ListBox>
</div>
</form>
</body>
</html>
然后是具體的邏輯代碼實(shí)現(xiàn)
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 DownFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//the first time to load
{
//get all the file in File folder
String[] AllTxt = Directory.GetFiles(Server.MapPath("File"));
foreach (String name in AllTxt)
{
ListBox1.Items.Add(Path.GetFileName(name));
}
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//make use of sssion to save the selected file in the listbox with the key of "select"
Session["select"] = ListBox1.SelectedValue.ToString();
}
protected void ImageButton_Down_Click(object sender, ImageClickEventArgs e)
{
//judge weather user choose at least one file
if (ListBox1.SelectedValue != "")
{
//get the path of the choosed file
String FilePath = Server.MapPath("File/") + Session["select"].ToString();
//initial the object of Class FileInfo and make it as the package path
FileInfo filepathinfo = new FileInfo(FilePath);
//judge weather the file exists
if (filepathinfo.Exists)
{
//save the file to local
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filepathinfo.Name));
Response.AddHeader("Content-length", filepathinfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(filepathinfo.FullName);
Response.End();
}
else
{
Page.RegisterStartupScript("sb", "<script>alert('Please choose one file,sir!')</script>");
}
}
}
protected void ImageButton_Up_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("Default.aspx");
}
}
注意:
最終的上傳的文件將會(huì)在根目錄下的File文件夾下看到,下載的時(shí)候也是從這個(gè)文件夾下進(jìn)行下載的。
總結(jié):
經(jīng)過(guò)這個(gè)小項(xiàng)目的實(shí)踐,我看到了session給編程帶來(lái)的便利,也體會(huì)到了FileUpload控件的威力;然而這并不是全部,這里僅僅是冰山一角而已,希望大家繼續(xù)學(xué)習(xí),一起進(jìn)步一起提高!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式)
- asp.net+jquery.form實(shí)現(xiàn)圖片異步上傳的方法(附j(luò)query.form.js下載)
- Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能實(shí)例教程
- asp.net 多文件上傳,兼容IE6/7/8,提供完整代碼下載
- asp.net 上傳或下載當(dāng)文件名包含有特殊字符"#"的處理
- asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼
- asp.net Web Services上傳和下載文件(完整代碼)
- ASP.NET中文件上傳下載方法集合
- ASP.NET實(shí)現(xiàn)文件上傳功能
- ASP.NET Core實(shí)現(xiàn)文件上傳和下載
相關(guān)文章
ASP.NET中控件的EnableViewState屬性及徹底禁用
如果我們?cè)陂_(kāi)發(fā)Web應(yīng)用程序時(shí),某些控件是不需要接受用戶的操作或只需要接受一次操作的時(shí)候,我們可以將這些控件的EnableViewState屬性改為false,這樣可以優(yōu)化我們的程序,提高網(wǎng)絡(luò)訪問(wèn)的速度。2016-06-06
Asp.net FileUpload上傳文件夾并檢測(cè)所有子文件的實(shí)現(xiàn)代碼
這篇文章主要介紹了Asp.net FileUpload上傳文件夾并檢測(cè)所有子文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-05-05
最簡(jiǎn)單的.NET生成隨機(jī)數(shù)函數(shù)
眾所周知 .Net中Random類(lèi)生成的隨機(jī)數(shù)是假隨機(jī)數(shù),關(guān)鍵要看構(gòu)造函數(shù)里的種子2009-05-05
不可忽視的 .NET 應(yīng)用5大性能問(wèn)題
ASP.NET 或是 Windows Forms 容器中,使用 ADO 庫(kù)與運(yùn)行在 CLR 交互,而 CLR 運(yùn)行在操作系統(tǒng)中而該硬件又與其他包含不同技術(shù)堆棧的硬件通過(guò)網(wǎng)絡(luò)相連。在你的應(yīng)用與外部環(huán)境之間,。我們還有 API 管理服務(wù)以及多級(jí)緩存基礎(chǔ)構(gòu)造數(shù)量龐雜,都可能影響應(yīng)用程序的性能!2016-05-05
使用aspnet_regiis.exe重新注冊(cè).NET Framework
本文主要介紹使用aspnet_regiis.exe重新注冊(cè).NET Framework的方法,簡(jiǎn)單實(shí)用,有需要的朋友拿去用吧。2016-05-05

