C# 后台获取请求来源、文件下载

 文件流下载文件

 void BigFileDownload()
        {
            try
            {
                string FileName = "测试.docx";
                string filePath = Page.Server.MapPath("~/Content/测试.docx");
                //以字符流的形式下载文件
                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;charset=gb2321";

                //通知浏览器下载文件而不是打开;对中文名称进行编码
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                Response.Flush();
                Response.End();
            }
        }

asp.net mvc 获取请求源

 /// 
        /// 下载APP
        /// 
        /// 
        [AllowAnonymous]
        [Route("download/app")]
        public ActionResult DownloadApp() {

            string android = "android";
            //获取客服端请求源
            string agent = Request.UserAgent;
            string[] keywords = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };

            // CloudWave.Framework.Core.Logging.Logging4net.WriteInfo(agent);
            //排除 Windows 桌面系统           
            //if (!agent.Contains("Windows NT") || (agent.Contains("Windows NT") && agent.Contains("compatible; MSIE 9.0;")))
            var downloadurl = "";
            //判断是微信, 直接响应应用宝地址
            if (agent.ToLower().Contains("micromessenger")) {
                //获取配置信息
                var wxUrl = ConfigurationManager.AppSettings["YingYongBao"];
                if (!string.IsNullOrEmpty(wxUrl)) {
                    return Redirect(wxUrl);
                }
                return RedirectToAction("index");
            }
            if (agent.ToLower().Contains(android)) {
                downloadurl = ConfigurationManager.AppSettings["AndroidUrl"];
                if (string.IsNullOrEmpty(downloadurl)) {
                    return RedirectToAction("index");
                }
            } else {
                downloadurl = ConfigurationManager.AppSettings["IOSUrl"];
                if (string.IsNullOrEmpty(downloadurl)) {
                    return RedirectToAction("index");
                }
                if (agent.ToLower().Contains("weibo")) {
                    downloadurl = "itms-apps" + downloadurl.Substring(downloadurl.IndexOf(":")); //itms-apps://
                }
            }
            return Redirect(downloadurl);
        }

 

转载于:https://www.cnblogs.com/miskis/p/7097954.html

你可能感兴趣的:(C# 后台获取请求来源、文件下载)