Asp.net File Download instance

Source: Internet
Author: User

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:

 

  1. A file path field is designed to store the virtual path of the server where the file is located in the database;
  2. 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;
  3. 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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.