string fname = "系统操作手册.doc"; string fullPath = HttpContext.Current.Request.PhysicalApplicationPath + "Asset\\SysHelp\\" + PublicMethod.AppSetting("AssetVesion") + "\\" + fname; bool blRet = true; if (File.Exists(fullPath)) { try { Response.AddHeader("Accept-Ranges", "bytes"); Response.Buffer = false; FileStream activeXfile = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(activeXfile); long fileLen = activeXfile.Length; long startBytes = 0; double pack = 10240; int sleep = (int)Math.Floor(1000 * pack / 1024000) + 1; if (Request.Headers["Range"] != null) { Response.StatusCode = 206; string[] range = Request.Headers["Range"].Split(new char[] { '=', '-' }); startBytes = Convert.ToInt64(range[1]); } Response.AddHeader("Content-Length", (fileLen - startBytes).ToString()); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8)); br.BaseStream.Seek(startBytes, SeekOrigin.Begin); int maxCount = (int)Math.Floor((fileLen - startBytes) / pack) + 1; for (int i = 0; i < maxCount; i++) { if (Response.IsClientConnected) { Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString()))); Thread.Sleep(sleep); } else { i = maxCount; } } } catch { blRet = false; } } else { blRet = false; } return blRet;
这样下载文件,在VS里调试可以下载,部署到IIS上就不能下载
文件权限已经给了everyone,完全控制