Recently, I was busy with my graduation project. One of my graduation project's links will download files from the server. When we browse the website, we occasionally download some materials, this download function is what I want to implement. The following describes my Implementation ideas:
- A file path field is designed to store the virtual path of the server where the file is located in the database;
- When uploading files in the background, copy the files to the fixed folder on the server, and add a record of the virtual storage path to the database;
- When you browse the website at the front-end, click the corresponding link to view the virtual path in the database, and then trigger the download (SAVE) event.
HTML code
<Form ID = "form1" runat = "server"> <asp: gridview id = "gvresource" runat = "server" width = "700px" autogeneratecolumns = "false"> <columns> <asp: boundfield datafield = "ID" headertext = "file number"/> <asp: boundfield datafield = "name" headertext = "file name"/> <asp: boundfield datafield = "type" headertext = "file type"/> <asp: boundfield datafield = "createtime" headertext = "Creation Time"/> <asp: boundfield datafield = "remark" headertext = "Remarks"/> <asp: templatefield headertext = "resource download"> <itemtemplate> <asp: linkbutton id = "lbtndown" runat = "server" oncommand = "lbtndown_click" commandargument = '<% # eval ("Address "). tostring () %> '> downlink </ASP: linkbutton> </itemtemplate> </ASP: templatefield> </columns> <rowstyle horizontalalign = "center"/> </ASP: gridview> </form>
Interface
CS code
Protected void lbtndown_click (Object sender, commandeventargs e) {// define the file path string url = ""; // define the file name string filename = ""; // obtain the address of the file on the server url = E. commandargument. tostring (); // response. write (E. commandargument. tostring (); // get the file name in the address # region get the file name in the address // determine whether the transfer address is empty if (url = "") {// The system prompts "this file is not available for download now" page. clientscript. registerstartupscript (page. getType (), "message", "<SCRIPT defer> alert ('the file is not available for download now! '); </SCRIPT> "); return;} // determines whether the obtained URL is an address, not a file name if (URL. indexof ("\")>-1) {// get the filename = URL. substring (URL. lastindexof ("\") + 1); // get the file name} When else {// URL is the file name, get the file name filename = URL;} # endregion/response. write (filename); // stream download file # region stream download file try {// define the server path string urlserver = server. mappath (URL); // download the file filestream = new filestream (urlserver, filemode. open); byte [] bytes = new byte [(INT) filestream. length]; filestream. read (bytes, 0, bytes. length); filestream. close (); response. contenttype = "application/octet-stream"; // notifies the browser of downloading instead of opening response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. text. encoding. utf8); response. binarywrite (bytes); response. flush (); response. end ();} catch (exception) {}# endregion}
Of course, here I just used a method-stream download. There are many ways to implement this function, such as writefile multipart download. If you are interested, you can study it yourself.