2024年3月7日发(作者:)

case "HEAD": break; default: Code = 501; return false; } if (!(filePath)) { Code = 404; return false; } long startBytes = 0; int packSize = 1024 * 10; //read in block,every block 10K bytes string fileName = eName(filePath); FileStream myFile = new FileStream(filePath, , , ite); BinaryReader br = new BinaryReader(myFile); long fileLength = ; int sleep = (int)g(1000.0 * packSize / speed);//the number of millisecond string lastUpdateTiemStr = tWriteTimeUtc(filePath).ToString("r"); string eTag = ode(fileName, 8) + lastUpdateTiemStr; //validate whether the file is too large if ( > ue) { Code = 413; return false; } if (s["If-Range"] != null) { if (s["If-Range"].Replace(""", "") != eTag) { Code = 412; return false; } } try { #region -------添加重要响应头、解析请求头、相关验证------------------- (); Output = false; //der("Content-MD5", GetMD5Hash(myFile)); der("Accept-Ranges", "bytes"); Header("ETag", """ + eTag + """); Header("Last-Modified", lastUpdateTiemStr); tType = "application/octet-stream"; der("Content-Disposition", "attachment;filename=" + ode(fileName, 8).Replace("+", "%20")); der("Content-Length", (fileLength - startBytes).ToString()); der("Connection", "Keep-Alive"); tEncoding = 8; if (s["Range"] != null) { Code = 206; string[] range = s["Range"].Split(new char[] { '=', '-' }); startBytes = 64(range[1]); if (startBytes < 0 || startBytes >= fileLength) { return false;

} } if (startBytes > 0) { // 如果是续传请求,告诉客户端本次的开始字节数,总长度,以便客户端将续传数据追加到startBytes位置后 der("Content-Range", (" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength)); } #endregion #region -------向客户端发送数据块------------------- //send data

(startBytes, ); int maxCount = (int)g((fileLength - startBytes + 0.0) / packSize);//download in block for (int i = 0; i < maxCount && ntConnected; i++) { Write(tes(packSize)); (); if (sleep > 1) (sleep); } #endregion } catch { ret = false; } finally { (); (); } } catch { ret = false; } return ret; }