HTTP請求下載文件
/// <summary> /// Http方式下載文件 /// </summary> /// <param name="url">url地址</param> /// <param name="path">路徑</param> /// <param name="fileName">文件名</param> /// <returns></returns> public static bool HttpDownLoadold(string url, string path, string fileName) { bool flag = false; long startPosition = 0; // 上次下載的文件起始位置 FileStream writeStream; // 寫入本地文件流對象 // 判斷文件夾是否存在 if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } //判斷文件是否存在 if (File.Exists(fileName)) { writeStream = File.OpenWrite(fileName); // 存在則打開要下載的文件 startPosition = writeStream.Length; // 獲取已經下載的長度 writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件寫入位置定位 } else { writeStream = new FileStream(path + fileName, FileMode.Create);// 文件不保存創建一個文件 startPosition = 0; } try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打開網絡連接 if (startPosition > 0) { myRequest.AddRange((int)startPosition);// 設置Range值,與上面的writeStream.Seek用意相同,是為了定義遠程文件讀取位置 } Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服務器請求,獲得服務器的回應數據流 byte[] btArray = new byte[512];// 定義一個字節數據,用來向readStream讀取內容和向writeStream寫入內容 int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向遠程文件讀第一次 while (contentSize > 0)// 如果讀取長度大於零則繼續讀 { writeStream.Write(btArray, 0, contentSize);// 寫入本地文件 contentSize = readStream.Read(btArray, 0, btArray.Length);// 繼續向遠程文件讀取 } //關閉流 writeStream.Close(); readStream.Close(); flag = true; //返回true下載成功 } catch (Exception) { writeStream.Close(); flag = false; //返回false下載失敗 } return flag; }
文件流下載文件
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 獲取請求源
/// <summary> /// 下載APP /// </summary> /// <returns></returns> [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); }