在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
今天看到了一篇不错的文章,就拿来一起分享一下吧。 关于文件上传: <system.web> <httpRuntime executionTimeout="240" maxRequestLength="20480"/> </system.web> 但是这种方式虽然可以自定义文件的大小,但并不是无极限的修改的 下一步,现在“工具”有了,要怎么上传呢?按照直觉是不是应该先选中我想要上传的文件呢?这就对了,因为从FileUpload控件返回后我们便已经得到了在客户端选中的文件的信息了,接下来就是将这个文件进行修改(具体的操作是:去掉所得路径下的盘符的信息,换成服务器上的相关路径下,不过这里并没有更改原本文件的名称)。然后调用相关的上传方法就好了。 先看一下界面文件吧 <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"); } } 说完了上传,下面谈一谈文件的下载。这里主要是借助于Directory对象的GetFiles()方法,其可以获得指定路径下的所有的文件的名称。这样我们就可以用之来填充一个listBox,来供我们选择到底要下载那一个文件。 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> 然后是具体的逻辑代码实现 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"); } } 注意: 总结: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。 |
请发表评论