diff --git a/WebServer/WebServer.cs b/WebServer/WebServer.cs index ad6d0f4..71030ee 100644 --- a/WebServer/WebServer.cs +++ b/WebServer/WebServer.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2020 Laurent Ellerbach and the project contributors // See LICENSE file in the project root for full license information. // @@ -451,7 +451,44 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s { throw e; } + } + + /// + /// Send file content over HTTP response. + /// + /// to send the content over. + /// Name of the file to send over . + /// Content of the file to send. + public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content) + { + string ContentType = GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))); + + try + { + response.ContentType = ContentType; + response.ContentLength64 = content.Length; + + // Now loop to send all the data. + for (long bytesSent = 0; bytesSent < content.Length;) + { + // Determines amount of data left + long bytesToSend = content.Length - bytesSent; + bytesToSend = bytesToSend < MaxSizeBuffer ? bytesToSend : MaxSizeBuffer; + + // Writes data to output stream + response.OutputStream.Write(content, (int)bytesSent, (int)bytesToSend); + + // allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel + + // update bytes sent + bytesSent += bytesToSend; + } + } + catch (Exception e) + { + throw e; + } } private void StartListener() @@ -621,7 +658,6 @@ private void ListInterfaces() } } - /// Get the MIME-type for a file name. /// /// Get the MIME-type for a file name. ///