Пример #1
0
        public async Task DownloadLogAsync(HttpListenerRequest request, HttpListenerResponse response, string logName, long limit)
        {
            string logFileName = logName + ".log";

            using (FileStream fS = new FileStream(Path.Combine(ConvertToAbsolutePath(_logFolder), logFileName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                response.ContentType = "text/plain";
                response.AddHeader("Content-Disposition", "attachment;filename=" + logFileName);

                if ((limit > fS.Length) || (limit < 1))
                {
                    limit = fS.Length;
                }

                OffsetStream oFS = new OffsetStream(fS, 0, limit);

                using (Stream s = DnsWebService.GetOutputStream(request, response))
                {
                    await oFS.CopyToAsync(s);

                    if (fS.Length > limit)
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes("####___TRUNCATED___####");
                        s.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }