二维码扫描下载
后端:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace ScanCodeDownload.Controllers { public class HomeController : Controller { /// <summary> /// 下载 /// </summary> /// <returns></returns> public JsonResult DownloadPeiZai() { Download.ResponseFile(@"C:\ScanCodeDownload\File\peizai.apk"); return null; } /// <summary> /// 二维码显示首页 /// </summary> /// <returns></returns> public ActionResult Index() { return View(); } } }
下载帮助类
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Web; 7 8 namespace ScanCodeDownload 9 { 10 public class Download 11 { 12 13 public Download() 14 { 15 } 16 17 public static bool ResponseFile(string fullPath) 18 { 19 HttpRequest request = HttpContext.Current.Request; 20 HttpResponse response = HttpContext.Current.Response; 21 try 22 { 23 FileInfo info = new FileInfo(fullPath); 24 if (info.Exists) 25 { 26 string name = info.Name; 27 string extension = info.Extension; 28 name = name.Substring(0, name.Length - extension.Length); 29 while (HttpUtility.UrlEncode(name + extension, Encoding.UTF8).Length > 0x9c) 30 { 31 name = name.Substring(0, name.Length - 1); 32 } 33 name = HttpUtility.UrlEncode(name + extension, Encoding.UTF8); 34 response.Clear(); 35 if (info.Length < 1024000)//0xfa000L) 36 { 37 response.ContentType = "application/octet-stream"; 38 response.AddHeader("Content-Disposition", "attachment;filename=" + name); 39 response.WriteFile(info.FullName); 40 } 41 else 42 { 43 FileStream input = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 44 BinaryReader reader = new BinaryReader(input); 45 try 46 { 47 long length = input.Length; 48 long num2 = 0L; 49 int count = 10240;// 0x2800; 50 if (request.Headers["Range"] != null) 51 { 52 response.StatusCode = 0xce; 53 num2 = Convert.ToInt64(request.Headers["Range"].Split(new char[] { '=', '-' })[1]); 54 } 55 response.AddHeader("Content-Length", (length - num2).ToString()); 56 if (num2 != 0L) 57 { 58 response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", num2, length - 1L, length)); 59 } 60 response.ContentType = "application/octet-stream"; 61 response.AddHeader("Content-Disposition", "attachment;filename=" + name); 62 reader.BaseStream.Seek(num2, SeekOrigin.Begin); 63 int num4 = ((int)Math.Floor((double)(((double)(length - num2)) / ((double)count)))) + 1; 64 for (int i = 0; i < num4; i++) 65 { 66 if (response.IsClientConnected) 67 { 68 response.BinaryWrite(reader.ReadBytes(count)); 69 response.Flush(); 70 } 71 else 72 { 73 i = num4; 74 } 75 } 76 } 77 catch 78 { 79 return false; 80 } 81 finally 82 { 83 reader.Close(); 84 input.Close(); 85 } 86 } 87 } 88 } 89 catch (Exception exception) 90 { 91 92 return false; 93 } 94 return true; 95 } 96 97 } 98 }
前端:
下载二维码JS:http://www.jq22.com/jquery-info294
注意事项:
$('#show_ecode').qrcode({ width: 92, height: 92, correctLevel: 0, text: "http://15f8n80281.iok.la/home/DownloadPeiZai" });地址必须是服务器带IP完整地址
@{ ViewBag.Title = "Index"; Layout = null; } <html> <body> <div id='show_ecode' style="text-align: center; width: 92px; height: 92px; margin: 0px auto; margin-top: 50px; "></div> </body> </html> <script src="~/Scripts/jquery-1.11.1.min.js"></script> <script src="~/Scripts/qrcode/jquery.qrcode.min.js"></script> <script type="text/javascript"> $(function () { if (document.all)//ie $('#show_ecode').qrcode({ width: 92, height: 92, render: "table", text: "http://15f8n80281.iok.la/home/DownloadPeiZai" }); else { $('#show_ecode').qrcode({ width: 92, height: 92, correctLevel: 0, text: "http://15f8n80281.iok.la/home/DownloadPeiZai" }); } }); </script>
项目下载地址:
http://pan.baidu.com/s/1misJwSw
浙公网安备 33010602011771号