本文整理汇总了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.");
}
}
示例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.");
}
}
示例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.");
}
}