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


C# HttpResponse.StreamFileASync方法代码示例

本文整理汇总了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);
			}
		}
开发者ID:soywiz,项目名称:NodeNetAsync,代码行数:101,代码来源:HttpStaticFileServer.cs


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