Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebServer - correct MIME-Type for images #1214 #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions nanoFramework.WebServer/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
/// /// <param name="contentType">The type of file, if empty string, then will use auto detection</param>
public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content, string contentType = "")
{
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))) : contentType;
contentType = contentType == "" ? GetContentTypeFromFileName(fileName) : contentType;
response.ContentType = contentType;
response.ContentLength64 = content.Length;

Expand Down Expand Up @@ -680,45 +680,71 @@ private void ListInterfaces()
/// <returns>The MIME-type for the file name.</returns>
private static string GetContentTypeFromFileName(string fileName)
{
var ext = fileName;
if (fileName.Contains("."))
{
ext = fileName.Substring(fileName.LastIndexOf('.'));
}
// normalize to lower case to speed comparison
fileName = fileName.ToLower();
ext = ext.ToLower().TrimStart('.');

string contentType = "text/html";

//determine the type of file for the http header
if (fileName == ".cs" ||
fileName == ".txt" ||
fileName == ".csproj"
if (ext == "cs" ||
ext == "txt" ||
ext == "csproj"
)
{
contentType = "text/plain";
}
else if (fileName == ".jpg" ||
fileName == ".bmp" ||
fileName == ".jpeg" ||
fileName == ".png"
)
else if (ext == "jpg" ||
ext == "jpeg"
)
{
contentType = "image/jpeg";
}
else if (ext == "bmp" ||
ext == "gif" ||
ext == "png"
)
{
contentType = "image";
contentType = $"image/{ext}";
}
else if (fileName == ".htm" ||
fileName == ".html"
)
else if (ext == "htm" ||
ext == "html"
)
{
contentType = "text/html";
}
else if (fileName == ".mp3")
else if (ext == "mp3")
{
contentType = "audio/mpeg";
}
else if (fileName == ".css")
else if (ext == "css")
{
contentType = "text/css";
}
else if (fileName == ".ico")
else if (ext == "ico")
{
contentType = "image/x-icon";
}
else if (ext == "xlm")
{
contentType = "text/xml";
}
else if (ext == "svg")
{
contentType = "image/svg+xml";
}
else if (ext == "js")
{
contentType = "text/javascript";
}
else if (ext == "json")
{
contentType = "application/json";
}

return contentType;
}
Expand Down