Example #1
0
        /// <summary>
        /// 输出文件
        /// </summary>
        /// <param name="response">回复对象</param>
        public void ExecuteResult(HttpResponse response)
        {
            if (string.IsNullOrEmpty(this.ContentType))
            {
                this.ContentType = "application/ocelet-stream";
            }
            response.Charset            = null;
            response.ContentType        = this.ContentType;
            response.ContentDisposition = this.ContentDisposition;

            using (var stream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read))
            {
                const int size  = 8 * 1024;
                var       state = response.WriteHeader((int)stream.Length);

                while (state == true)
                {
                    var bytes  = new byte[size];
                    var length = stream.Read(bytes, 0, size);
                    if (length == 0)
                    {
                        break;
                    }
                    var content = new ByteRange(bytes, 0, length);
                    state = response.WriteContent(content);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 输出文件
        /// </summary>
        /// <param name="response">回复对象</param>
        public void ExecuteResult(HttpResponse response)
        {
            if (string.IsNullOrEmpty(this.ContentType))
            {
                this.ContentType = "application/ocelet-stream";
            }
            response.Charset = null;
            response.ContentType = this.ContentType;
            response.ContentDisposition = this.ContentDisposition;

            using (var stream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read))
            {
                const int size = 8 * 1024;               
                var state = response.WriteHeader((int)stream.Length);

                while (state == true)
                {
                    var bytes = new byte[size];
                    var length = stream.Read(bytes, 0, size);
                    if (length == 0)
                    {
                        break;
                    }
                    var content = new ByteRange(bytes, 0, length);
                    state = response.WriteContent(content);
                }
            }
        }
Example #3
0
        /// <summary>
        /// 输出文件
        /// Gzip压缩
        /// </summary>
        /// <param name="response"></param>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        private async Task ResponseFileByGzipAsync(HttpResponse response, FileStream fileStream)
        {
            var buffer = new byte[fileStream.Length];
            await fileStream.ReadAsync(buffer, 0, buffer.Length);

            var zipBuffer = GZip.Compress(buffer);

            response.WriteHeader(zipBuffer.Length, true);
            response.WriteContent(zipBuffer);
        }
Example #4
0
        /// <summary>
        /// 输出文件
        /// Gzip压缩
        /// </summary>
        /// <param name="response"></param>
        /// <param name="fileStream"></param>
        private void ResponseFileByGzip(HttpResponse response, FileStream fileStream)
        {
            var buffer = new byte[fileStream.Length];

            fileStream.Read(buffer, 0, buffer.Length);

            var zipBuffer = GZip.Compress(buffer);

            response.WriteHeader(zipBuffer.Length, true);
            response.WriteContent(zipBuffer);
        }
Example #5
0
        /// <summary>
        /// 输出文件
        /// </summary>
        /// <param name="response"></param>
        /// <param name="fileStream"></param>
        private void ResponseFile(HttpResponse response, FileStream fileStream)
        {
            const int size  = 8 * 1024;
            var       state = response.WriteHeader((int)fileStream.Length);
            var       bytes = new byte[size];

            while (state == true)
            {
                var length = fileStream.Read(bytes, 0, size);
                if (length == 0)
                {
                    break;
                }
                var content = new ArraySegment <byte>(bytes, 0, length);
                state = response.WriteContent(content);
            }
        }