拥有网页版小U盘 ASP.NET实现文件上传与下载功能
今天看到了一篇不错的文章,就拿来一起分享一下吧。
实现的是文件的上传与下载功能。
关于文件上传:
谈及文件上传到网站上,首先我们想到的就是通过什么上传呢?在asp.net中,只需要用fileupload控件即可完成,但是默认上传4m大小的数据,当然了你可以在web.config文件中进行修改,方式如下:
<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,来供我们选择到底要下载那一个文件。
也许这时你会有一点疑惑了,我现在知道了有哪些文件可以下载,那下一步我要怎么来实现呢?
其实这里是利用了session的存储机制,那就是将我们在listbox 中选择的item的内容记录到session的特定的key中,这样的话,我们就可以不用关心这些信息在页面间是怎么传输的了。只需要在想要进行下载的地方直接获取就可以了。
最为核心的是下载的过程:
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"); } }
注意:
最终的上传的文件将会在根目录下的file文件夹下看到,下载的时候也是从这个文件夹下进行下载的。
总结:
经过这个小项目的实践,我看到了session给编程带来的便利,也体会到了fileupload控件的威力;然而这并不是全部,这里仅仅是冰山一角而已,希望大家继续学习,一起进步一起提高!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。