Backstage
1 public void DownFiletemple(string filepath) 2 { 3 FileInfo fileInfo = new FileInfo(filepath); 4 Response.Clear(); 5 Response.ClearContent(); 6 Response.ClearHeaders(); 7 logger.Trace("测试1:···" + fileInfo.FullName); 8 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.FullName); 9 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 10 logger.Trace("测试2:··Content-Length/·" + fileInfo.Length.ToString()); 11 Response.AddHeader("Content-Transfer-Encoding", "binary"); 12 Response.AddHeader("Connection", "Keep-Alive"); 13 14 Response.ContentType = "application/octet-stream"; 15 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 16 logger.Trace("测试3"); 17 Response.WriteFile(fileInfo.FullName); 18 logger.Trace("成功"); 19 Response.Flush(); 20 Response.End(); 21 22 23 }
Asynchronous request
1 function btnfiledwon () { 2 3 4 var url = ' vshop_CheckOut.aspx? Type = ajax & action = FileDown ' ; 5 6 var xhr = new XMLHttpRequest (); 7 8 xhr.open ( ' POST ' , url, true ); / / You can also use the POST method, according to the interface 9 10 xhr.responseType = " blob " ; // Return type blob 11 12 // Define the processing function of the request completion, you can also add a load box before the request / disable the download button logic 13 14 xhr.onload = function () { 15 // Request complete 16 17 if ( this .status === 200 ) { 18 // Return 200 19 20 var blob = this .response; 21 22 var reader = new FileReader (); 23 24 reader.readAsDataURL (blob); // Convert to base64, you can directly put a emoticon href 25 26 reader.onload = function (e) { 27 28 // Conversion is completed, create an a tag for download 29 30 var a = document.createElement ( ' a ' ); 31 32 a.download = ' Order contract.docx ' ; 33 34 a.href = e.target.result; 35 36 $ ( " body " ) .append (a) ; // Fix the click 37 38 a.click (); 39 40 $ (a) .remove (); 41 42 } 43 44 } 45 46 }; 47 48 // Send ajax request 49 50 xhr.send () 51 }