如何用ASP.NET开发网页版小U盘实现高效文件上传下载功能?

2026-03-31 05:231阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计779个文字,预计阅读时间需要4分钟。

今天看到了一篇不错的文章,就拿来分享一下吧。实现的其实是文件的上传与下载功能。关于文件上传:

谈及文件上传,首先我们想到的可能是将文件上传到网站上。在ASP.NET中,只用到这个方法即可。

今天看到了一篇不错的文章,就拿来一起分享一下吧。
实现的是文件的上传与下载功能。

关于文件上传:
谈及文件上传到网站上,首先我们想到的就是通过什么上传呢?在ASP.NET中,只需要用FileUpload控件即可完成,但是默认上传4M大小的数据,当然了你可以在web.config文件中进行修改,方式如下:

<system.web> <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" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:ImageButton ID="ImageButton_Down" runat="server" Height="52px" OnClick="ImageButton_Down_Click" style="margin-top: 0px" ToolTip="Download" Width="107px" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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>

然后是具体的逻辑代码实现

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"); } }

注意:
最终的上传的文件将会在根目录下的File文件夹下看到,下载的时候也是从这个文件夹下进行下载的。

总结:
经过这个小项目的实践,我看到了session给编程带来的便利,也体会到了FileUpload控件的威力;然而这并不是全部,这里仅仅是冰山一角而已,希望大家继续学习,一起进步一起提高!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计779个文字,预计阅读时间需要4分钟。

今天看到了一篇不错的文章,就拿来分享一下吧。实现的其实是文件的上传与下载功能。关于文件上传:

谈及文件上传,首先我们想到的可能是将文件上传到网站上。在ASP.NET中,只用到这个方法即可。

今天看到了一篇不错的文章,就拿来一起分享一下吧。
实现的是文件的上传与下载功能。

关于文件上传:
谈及文件上传到网站上,首先我们想到的就是通过什么上传呢?在ASP.NET中,只需要用FileUpload控件即可完成,但是默认上传4M大小的数据,当然了你可以在web.config文件中进行修改,方式如下:

<system.web> <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" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:ImageButton ID="ImageButton_Down" runat="server" Height="52px" OnClick="ImageButton_Down_Click" style="margin-top: 0px" ToolTip="Download" Width="107px" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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>

然后是具体的逻辑代码实现

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"); } }

注意:
最终的上传的文件将会在根目录下的File文件夹下看到,下载的时候也是从这个文件夹下进行下载的。

总结:
经过这个小项目的实践,我看到了session给编程带来的便利,也体会到了FileUpload控件的威力;然而这并不是全部,这里仅仅是冰山一角而已,希望大家继续学习,一起进步一起提高!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。