/// <summary>
/// Serves the given file to the client and closes the request when done.
/// </summary>
/// <param name="res">The res.</param>
/// <param name="path">The path.</param>
/// <exception cref="System.ArgumentException">File does not exist</exception>
public static void ServeFile(this IServerResponse res, string path)
{
if (!File.Exists(path))
{
throw new ArgumentException("File does not exist");
}
if (!res.IsCached)
{
res.ContentLength64 = new FileInfo(path).Length;
}
var fileInfo = new FileInfo(path);
res.ContentLength64 = fileInfo.Length;
var stream = File.Open(path, FileMode.Open, FileAccess.Read);
var copier = new AsyncStreamCopier(stream, res.OutputStream);
copier.Completed += (s, e) =>
{
e.InputStream.Close();
res.Close();
};
copier.Copy();
}