// Download function download (ID) {var form =$ ("<form>"); // define a Form. ATTR ('style', 'display: none'); // Add the query parameter form to the form. ATTR ('target', ''); form. ATTR ('method', 'post'); form. ATTR ('action', "/task/detaildownload"); var input1 = $ ('<input>'); input1.attr ('type', 'hiddd '); input1.attr ('name', 'id'); input1.attr ('value', ID); $ ('body '). append (form); // place the form in the web form. append (input1); // submit the query parameter control to form. submit ();}
Code from the MVC Controller
# Region details-download public void detaildownload (int id) {model. attachment entity = aM. getbyattachmentid (ID); string filename = entity. attachmentname. split ('/') [1]; // string filepath = Common. confighelper. getconfigstring ("picupload") + "/attachment/" + entity. attachmentname; // download the file filestream FS = new filestream (filepath, filemode. open); byte [] bytes = new byte [(INT) FS. length]; FS. read (bytes, 0, bytes. length); FS. close (); response. contenttype = "application/octet-stream"; // notify the browser to download the file instead of opening response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. text. encoding. utf8); response. binarywrite (bytes); response. flush (); response. end () ;}# endregion
Download file: Ajax download Effect