本文整理汇总了C#中HttpResponse.StreamFileASync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponse.StreamFileASync方法的具体用法?C# HttpResponse.StreamFileASync怎么用?C# HttpResponse.StreamFileASync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.StreamFileASync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FilterAsync
//.........这里部分代码省略.........
FilePath = FilePath,
});
}
catch (FileNotFoundException)
{
throw (new HttpException(HttpCode.NOT_FOUND_404));
}
}
if (Cache.Enabled)
{
await Console.Out.WriteLineAsync(String.Format("Caching '{0}'", FilePath));
}
var FileInfo = await VirtualFileSystem.GetFileInfoAsync(FilePath);
if (FileInfo.Exists)
{
bool ShouldCacheInMemory = FileInfo.Length <= CacheSizeThresold;
string ETag = null;
byte[] Data = null;
if (ShouldCacheInMemory)
{
Data = await VirtualFileSystem.ReadAsBytesAsync(FilePath);
ETag = BitConverter.ToString(MD5.Create().ComputeHash(Data));
}
return new ResultStruct()
{
RealFilePath = FilePath,
ContentType = MimeType.GetFromPath(FilePath),
FileInfo = FileInfo,
ETag = ETag,
Data = Data,
Exists = true,
};
}
else
{
throw (new HttpException(HttpCode.NOT_FOUND_404));
}
});
Response.Headers["Content-Type"] = CachedResult.ContentType;
if (CachedResult.ETag != null)
{
Response.Headers["ETag"] = CachedResult.ETag;
}
Response.Headers["Date"] = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
Response.Headers["Last-Modified"] = CachedResult.FileInfo.LastWriteTimeUtc.ToString("R", CultureInfo.InvariantCulture);
Response.Headers["Cache-Control"] = "max-age=2419200, private";
Response.Headers["Expires"] = "Wed, 11 Apr 2022 18:23:41 GMT";
// Check ETag
if (Request.Headers["If-None-Match"] != "")
{
if (Request.Headers["If-None-Match"] == CachedResult.ETag)
{
throw(new HttpException(HttpCode.NOT_MODIFIED_304));
}
}
// Check Last-Modified
if (Request.Headers["If-Modified-Since"] != "")
{
var RequestIfModifiedSince = DateTime.ParseExact(Request.Headers["If-Modified-Since"], "R", CultureInfo.InvariantCulture);
if (RequestIfModifiedSince == CachedResult.FileInfo.LastWriteTimeUtc)
{
throw(new HttpException(HttpCode.NOT_MODIFIED_304));
}
}
Response.Buffering = true;
Response.ChunkedTransferEncoding = false;
/*
Cache-Control:max-age=2419200, private
Connection:keep-alive
Date:Wed, 14 Mar 2012 14:52:54 GMT
Expires:Wed, 11 Apr 2012 14:52:54 GMT
Last-Modified:Wed, 11 Jan 2012 13:52:46 GMT
*/
Response.Headers["Content-Length"] = CachedResult.FileInfo.Length.ToString();
// Cached byte[]
if (CachedResult.Data != null)
{
await Response.WriteAsync(CachedResult.Data);
}
// No cached byte[], stream the file
else
{
Response.Buffering = false;
//Response.ChunkedTransferEncoding = true;
await Response.StreamFileASync(CachedResult.RealFilePath);
}
}