/// <summary> /// 将文件从服务器下载到本地 /// </summary> /// <param name="fileName">文件路径</param> /// <param name="resId">资源id</param> /// <param name="appId">附件id</param> public void DownLoad(string fileName, string resId, string appId) { using (JSZX_ResourceEntities db = new JSZX_ResourceEntities()) { Resource res = new Resource(); string userId = CommonUtil.GetSession(Session, "id"); res.AddDownLoadNum(resId, appId, userId, db); } string loccalFile = fileName;// Constant.DISK_ADDRESS + fileName; try { if (!System.IO.File.Exists(loccalFile)) { throw new Exception("文件不存在"); } //100K 每次读取文件,只读取100K //const long ChunkSize = 102400; //byte[] buffer = new byte[ChunkSize]; //HttpContext.Response.Clear(); //FileStream fs = new FileStream(loccalFile, FileMode.Open); //long dataLengthToRead = fs.Length;//获取下载的文件总大小 #region 20150228 5920 Add 文件ID改成文件原名 fileName = Path.GetFileName(fileName); Resource res = new Resource(); fileName = res.GetFileOriginalName(fileName); #endregion HttpContext.Response.ContentType = "application/octet-stream"; HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); Response.TransmitFile(loccalFile); //while (dataLengthToRead > 0 && HttpContext.Response.IsClientConnected) //{ // int lengthRead = fs.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 // HttpContext.Response.OutputStream.Write(buffer, 0, lengthRead); // HttpContext.Response.Flush(); // dataLengthToRead = dataLengthToRead - lengthRead; //} //HttpContext.Response.Close(); ////下载完成 //if (dataLengthToRead == 0) //{ // if (fs != null) // { // fs.Close(); // } //} //下载资源时,可以获得积分 PointRule pr = new PointRule(); pr.AddPoint(resId, CommonUtil.GetSession(Session, "id"), "1"); //下载资源时,可以获得积分 } catch (Exception ex) { clsLog.ErrorLog("MineController", "DownLoad", ex.Message); HttpContext.Response.Write("<script type='text/javascript'>layer.alert(" + ex.Message + ")</script>"); } }