@@ -465,10 +465,11 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
465465 /// <param name="response"><see cref="HttpListenerResponse"/> to send the content over.</param>
466466 /// <param name="fileName">Name of the file to send over <see cref="HttpListenerResponse"/>.</param>
467467 /// <param name="content">Content of the file to send.</param>
468- /// /// <param name="contentType">The type of file, if empty string, then will use auto detection</param>
468+ /// <param name="contentType">The type of file, if empty string, then will use auto detection. </param>
469469 public static void SendFileOverHTTP ( HttpListenerResponse response , string fileName , byte [ ] content , string contentType = "" )
470470 {
471- contentType = contentType == "" ? GetContentTypeFromFileName ( fileName . Substring ( fileName . LastIndexOf ( '.' ) ) ) : contentType ;
471+ // If no extension, we will get the full file name
472+ contentType = contentType == "" ? GetContentTypeFromFileName ( fileName . Substring ( fileName . LastIndexOf ( '.' ) + 1 ) ) : contentType ;
472473 response . ContentType = contentType ;
473474 response . ContentLength64 = content . Length ;
474475
@@ -483,8 +484,6 @@ public static void SendFileOverHTTP(HttpListenerResponse response, string fileNa
483484 // Writes data to output stream
484485 response . OutputStream . Write ( content , ( int ) bytesSent , ( int ) bytesToSend ) ;
485486
486- // 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
487-
488487 // update bytes sent
489488 bytesSent += bytesToSend ;
490489 }
@@ -676,49 +675,58 @@ private void ListInterfaces()
676675 /// <summary>
677676 /// Get the MIME-type for a file name.
678677 /// </summary>
679- /// <param name="fileName ">File name to get content type for.</param>
678+ /// <param name="fileExt ">File extension to get content type for.</param>
680679 /// <returns>The MIME-type for the file name.</returns>
681- private static string GetContentTypeFromFileName ( string fileName )
680+ private static string GetContentTypeFromFileName ( string fileExt )
682681 {
683682 // normalize to lower case to speed comparison
684- fileName = fileName . ToLower ( ) ;
683+ fileExt = fileExt . ToLower ( ) ;
685684
686685 string contentType = "text/html" ;
687686
688687 //determine the type of file for the http header
689- if ( fileName == ".cs" ||
690- fileName == ".txt" ||
691- fileName == ".csproj"
692- )
688+ if ( fileExt == "cs" ||
689+ fileExt == "txt" ||
690+ fileExt == "csproj" )
693691 {
694692 contentType = "text/plain" ;
695693 }
696- else if ( fileName == ".jpg" ||
697- fileName == ".bmp" ||
698- fileName == ".jpeg" ||
699- fileName == ".png"
700- )
694+ else if ( fileExt == "jpg" ||
695+ fileExt == "jpeg" ||
696+ fileExt == "jpe" )
697+ {
698+ contentType = "image/jpeg" ;
699+ }
700+ else if ( fileExt == "bmp" ||
701+ fileExt == "png" ||
702+ fileExt == "gif" ||
703+ fileExt == "ief" )
701704 {
702- contentType = "image" ;
705+ contentType = $ "image/ { fileExt } ";
703706 }
704- else if ( fileName == ".htm" ||
705- fileName == ".html"
706- )
707+ else if ( fileExt == "htm" ||
708+ fileExt == "html" )
707709 {
708710 contentType = "text/html" ;
709711 }
710- else if ( fileName == ". mp3" )
712+ else if ( fileExt == "mp3" )
711713 {
712714 contentType = "audio/mpeg" ;
713715 }
714- else if ( fileName == ". css" )
716+ else if ( fileExt == "css" )
715717 {
716718 contentType = "text/css" ;
717719 }
718- else if ( fileName == ". ico" )
720+ else if ( fileExt == "ico" )
719721 {
720722 contentType = "image/x-icon" ;
721723 }
724+ else if ( fileExt == "zip" ||
725+ fileExt == "json" ||
726+ fileExt == "pdf" )
727+ {
728+ contentType = $ "application/{ fileExt } ";
729+ }
722730
723731 return contentType ;
724732 }
0 commit comments