当前位置: 首页>>代码示例>>C#>>正文


C# IHttpResponse.TransmitFile方法代码示例

本文整理汇总了C#中IHttpResponse.TransmitFile方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpResponse.TransmitFile方法的具体用法?C# IHttpResponse.TransmitFile怎么用?C# IHttpResponse.TransmitFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IHttpResponse的用法示例。


在下文中一共展示了IHttpResponse.TransmitFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ProcessRequest

        public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
        {
            var fileName = request.GetPhysicalPath();

            var fi = new FileInfo(fileName);
            if (!fi.Exists)
            {
                if ((fi.Attributes & FileAttributes.Directory) != 0)
                {
                    foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
                    {
                        var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
                        var defaultFileInfo = new FileInfo(defaultFileName);
                        if (!defaultFileInfo.Exists) continue;
                        response.Redirect(request.GetPathUrl() + '/' + defaultDoc);
                        return;
                    }
                }

                if (!fi.Exists)
                    throw new HttpException(404, "File '" + request.PathInfo + "' not found.");
            }

            var strHeader = request.Headers["If-Modified-Since"];
            try
            {
                if (strHeader != null)
                {
                    var dtIfModifiedSince = DateTime.ParseExact(strHeader, "r", null);
                    var ftime = fi.LastWriteTime.ToUniversalTime();
                    if (ftime <= dtIfModifiedSince)
                    {
                        response.ContentType = MimeTypes.GetMimeType(fileName);
                        response.StatusCode = 304;
                        return;
                    }
                }
            }
            catch { }

            try
            {
                var lastWT = fi.LastWriteTime.ToUniversalTime();
                response.AddHeader("Last-Modified", lastWT.ToString("r"));

                response.ContentType = MimeTypes.GetMimeType(fileName);

                if (!Env.IsMono)
                {
                    response.TransmitFile(fileName);
                }
                else
                {
                    response.WriteFile(fileName);
                }
            }
            catch (Exception)
            {
                throw new HttpException(403, "Forbidden.");
            }
        }
开发者ID:Braunson,项目名称:ServiceStack,代码行数:61,代码来源:StaticFileHandler.cs

示例2: ProcessRequest

        public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
        {
            var fileName = request.GetPhysicalPath();

            var fi = new FileInfo(fileName);
            if (!fi.Exists)
            {
                if ((fi.Attributes & FileAttributes.Directory) != 0)
                {
                    foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
                    {
                        var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
                        var defaultFileInfo = new FileInfo(defaultFileName);
                        if (!defaultFileInfo.Exists) continue;
                        response.Redirect(request.GetPathUrl() + '/' + defaultDoc);
                        return;
                    }
                }

                if (!fi.Exists)
                    throw new HttpException(404, "File '" + request.PathInfo + "' not found.");
            }

            if (request.HasNotModifiedSince(fi.LastWriteTime))
            {
                response.ContentType = MimeTypes.GetMimeType(fileName);
                response.StatusCode = 304;
                return;
            }

            try
            {
                response.AddHeaderLastModified(fi.LastWriteTime);
                response.ContentType = MimeTypes.GetMimeType(fileName);

                if (!Env.IsMono)
                {
                    response.TransmitFile(fileName);
                }
                else
                {
                    response.WriteFile(fileName);
                }
            }
            catch (Exception)
            {
                throw new HttpException(403, "Forbidden.");
            }
        }
开发者ID:bman654,项目名称:ServiceStack,代码行数:49,代码来源:StaticFileHandler.cs

示例3: ProcessRequest

        public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
        {
            ServiceStack.WebHost.Endpoints.EndpointHost.AddGlobalResponseHeaders(response);

            var fileName = request.GetPhysicalPath();

            var fi = new FileInfo(fileName);
            if (!fi.Exists)
            {
                if ((fi.Attributes & FileAttributes.Directory) != 0)
                {
                    foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments)
                    {
                        var defaultFileName = Path.Combine(fi.FullName, defaultDoc);
                        if (!File.Exists(defaultFileName)) continue;
                        response.Redirect(request.GetPathUrl() + '/' + defaultDoc);
                        return;
                    }
                }

                if (!fi.Exists)
                    throw new HttpException(404, "File '" + request.PathInfo + "' not found.");
            }

            TimeSpan maxAge;
            if (response.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(response.ContentType, out maxAge))
            {
                response.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds);
            }

            if (request.HasNotModifiedSince(fi.LastWriteTime))
            {
                response.ContentType = MimeTypes.GetMimeType(fileName);
                response.StatusCode = 304;
                return;
            }

            try
            {
                response.AddHeaderLastModified(fi.LastWriteTime);
                response.ContentType = MimeTypes.GetMimeType(fileName);

                if (fileName.EqualsIgnoreCase(this.DefaultFilePath))
                {
                    if (fi.LastWriteTime > this.DefaultFileModified)
                        SetDefaultFile(this.DefaultFilePath); //reload

                    response.OutputStream.Write(this.DefaultFileContents, 0, this.DefaultFileContents.Length);
                    response.Close();
                    return;
                }

                if (!Env.IsMono)
                {
                    response.TransmitFile(fileName);
                }
                else
                {
                    response.WriteFile(fileName);
                }
            }
            catch (Exception)
            {
                throw new HttpException(403, "Forbidden.");
            }
        }
开发者ID:assaframan,项目名称:ServiceStack,代码行数:66,代码来源:StaticFileHandler.cs


注:本文中的IHttpResponse.TransmitFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。