WCF下载文档
public Stream DownloadFileStream(string serverfile, string path, long offset = 0, long count = 0) { CusFileInfo file = null; var enumerator = files.Where((t) => t.MapPath == path "/" serverfile).GetEnumerator(); if (enumerator.MoveNext()) { file = enumerator.Current; } else { throw new FaultException("非法请求。"); } if (offset >= file.FileLength) { throw new FaultException("请求的断点超过了文件最大长度。"); } if (count == 0) { count = file.FileLength - offset; } if (File.Exists(file.filepath)) { var outgoingResponse = WebOperationContext.Current.OutgoingResponse; outgoingResponse.ContentType = "application/force-download"; Console.WriteLine("开始下载:" serverfile "(" offset "--" count ")"); CusStreamReader fs = new CusStreamReader(new FileStream(file.filepath, FileMode.Open, FileAccess.Read, FileShare.Read), offset, count); fs.Reading = (t) => { //限速代码,实际使用时可以去掉,或者精确控制 Thread.Sleep(300); Console.WriteLine(t); }; return fs; } else { throw new FaultException("没找到文件:" serverfile); } }
评论