Content-Disposition inline does not work in Chrome - google-chrome

I have a simple asp net core endpoint which return email file:
[HttpGet("files/{id:int}")]
public async Task<IActionResult> GetFileAsync(int id)
{
...
var fileName = $"{id}.msg";
var filePath = ...;
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
Inline = true,
};
Response.Headers.Add("Content-Disposition", cd.ToString());
return PhysicalFile(filePath, MimeTypes.GetMimeType(filePath));
}
I want that browser tries to open file and it works in Firefox. But Chrome only downloads file even with inline Content-Disposition:
Content-Disposition: inline; filename=10979.msg
Content-Length: 112128
Content-Type: application/octet-stream
Server: Microsoft-IIS/8.5
How to fix this?

How to fix this?
Firefox help user to open file with a dialog but Edge and Chrome doesn't.
But you could choose the "Always open files of this type" option as below to open files automatically.
Operation in Firefox, Edge and Chrome browser
How to Undo "always open files of this type"?
Chrome
Edge

Change Content-Type to application/msg

Related

how to use xhr.overrideMimeType in Chrome / IE Edge?

I have an issue with sending a file (part of a request in form data format).
The issue seems coming from the fact that only in Chrome for Linux the file (which is CVS file, with .csv extension and basically just text) is sent with mimetype (Content-type in request body) Content-Type: application/octet-stream
So, I am trying to override the mimetype to match the same sent by Chrome on Linux which is text/csv.
However the mimetype is apparently not overriden and still send as octet-stream.
My code:
let xhr = new XMLHttpRequest();
let form = new FormData();
form.append('file', file, file.name); // the file is loaded correctly
form.append('payload', JSON.stringify(data)); // data is just a JSON object
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
// we arrive here both on Debian and Windows 10
}
}
xhr.upload.onerror = function() { .... } // no error
xhr.open('POST', 'http://<my_url>', true);
console.log(file.type);
xhr.overrideMimeType("text/csv");
xhr.send(form);
A couple of notes:
console.log(file.type) actually prints "text-csv" but only in Chrome for Linux (Debian specifically). in the other cases (any other browser or platform) nothing is printed
given the previous point, it seems clear to me for some reason any other browser / platform can't recognize the file type, so the file is sent as octet-stream (general binary file)
xhr.overrideMimeType changes the MIME-type of the response, not the request.
I you want to change the MIME-type of the file, just create a new Blob with an explicit file type:
var blob = new Blob([blob], {type: 'text/csv'});
form.append('file', blob, file.name);
The above changes the MIME-type of the file in the uploaded form to "text/csv", as desired.
PS. If you literally want to change the MIME-type of the whole request (instead of just the file), use xhr.setRequestHeader('Content-Type', 'custom MIME type here');. (This only makes sense if you are sending a non-standard or custom data in the xhr.send method).

Css stylesheet loaded as html in Firefox

I've got a css file injected into my asp.net web form application page through a base page. The method I use look like the following:
private void InjectLocalStyleSheet()
{
if (this.Page.Header == null)
return;
Literal cssFile = new Literal()
{
Text =
#"<link rel=""stylesheet"" type=""text/css"" href=""" + Page.ResolveUrl("~/Common/Theme.css") +
#""" />"
};
Page.Header.Controls.Add(cssFile);
}
When I run the page in firefox, that css file gives me a 302 warning. Apparently, firefox view this file as html type while I've specified the type to be "text/css".
snap of request and response headers
I also run the web page in Chrome and get "Failed to load resouce: net::ERR_TOO_MANY_REDIRECTS"
Anyone has an idea about what is going on? Please help. Thank you.

Internet Explorer Enhanced Security Configuration(IEESC) related popup is coming after uploading project in IIS

I uploaded my project in IIS which was working fine in local but in windows server 2008 R2 it was showing the above attached issue after login(Please check the attached image). The above issue was coming because Internet Explorer Enhanced Security Configuration(IEESC) was on, so I make it off but still my page was not working.
Page Behavior: 1) No page error .Also no 404 and 403 error.(Even if CustomError mode is On)
2) Controls including grid view was not getting filled up from database by JSON call.
Solution: 1) Enable .json file extension simply follow this instructions. Open the properties for the server in IIS Manager and click MIME Types
Click "New". Enter "JSON" for the extension and "application/json" for the MIME type.
2) Add the following line in web.config file
3) If you deploy your application to IIS your URI must include your application name as well. So, as your application name is QCValueStream, then your URI must be http://localhost/QCValueStream/ManageProjects/GetManageProjectsData/5.
You can automatically detect your base Uri and have it prepend by adding a line in your master page(Asp.net web application) or shared _Layout.cshtml(Asp.net MVC):
<script type="text/javascript">
var config = {
contextPath: '#Url.Content("~")'
}
var baseUri = config.contextPath;
//or
var baseUri = '#Url.Content("~")';
//Then in your JS you prepend by:
//Incorrect JSON Call
$.getJSON('/ManageProjects/GetManageProjectsData?', { searchText: inputsearchText }, function (data) {
myData = data;
});
//Correct JSON Call
$.getJSON(baseUri +'/ManageProjects/GetManageProjectsData?', { searchText: inputsearchText }, function (data) {
myData = data;
});
</script>
Note: Check the below url to make off Explorer Enhanced Security Configuration for Administrator or user.
http://www.aurelp.com/2013/01/16/how-to-turn-off-internet-explorer-enhanced-security-configuration-step-by-step/

Browser not showing the SaveAs Dialog even after setting the Content-Disposition in response header

After reading several queries asked regarding setting Content-Disposition, I finally wrote the below code for sending a file such that Browser shows SaveAs dialog box. However, the Browser doesn't provide with the SaveAs dialog despite setting the "Content-Disposition" in the response header. I could see from the Browser's debug window the file's content and the response header does contain the "Content-Disposition".
I am clueless as to why the Browser is unable to show the SaveAs popup. Am I missing something here ?
Code:
#RequestMapping(value="...", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
#ResponseBody
public void getFile(HttpServletRequest request,
HttpServletResponse response,..) {
...
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// set headers for the response
String headerValue = String.format("attachment; filename=\"%s\"",
file.getName());
response.setHeader("Content-Disposition", headerValue);
// set content length
response.setContentLength((int) downloadFile.length());
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
inputStream.close();
...
}
The response header when checked from Google Chrome's debug window:
Content-Disposition:attachment; filename="ABCDEF.abc"
Content-Length:732
Content-Type:application/octet-stream;charset=UTF-8
Date:Wed, 04 Jun 2014 19:05:37 GMT
Server:Apache-Coyote/1.1
The html tag for downloading the file:
<div class="divCsvReportIcon">
<a class="a-csvReport" href="" target="#"
data-ng-click="fetchCSVData($event)" ng-show="pivotTabContentShow">
<img class="csv-img-icon" src="assets/icons/icon_csv-32.png" >
</a>
</div>
Other Info:
Spring 3.2.x
Angularjs based html client

403 error when opening PDF with chrome pdf viewer

We host C# 3.5 net web app in ISS 7.5.
PDF files are generated and put in some directory. Listing of directory is disabled. With all browsers (IE10, FF, Opera...) we can access the PDF.
When accessing the URL with Chrome, PDF is loaded... then we get a 403 error. If we disable chrome internal pdf viewer and tell it to use Adobe's, it works fine.
What can be wrong?
Problem is explained here: http://productforums.google.com/forum/#!topic/chrome/1mSjCjabwPE
But the mentioned KB cannot be applied so we'll do with some HttpHandler.
public void ProcessRequest(HttpContext context)
{
switch (context.Request.HttpMethod)
{
case "GET":
if (!context.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
return;
}
string requestedFile = context.Server.MapPath(context.Request.FilePath);
context.Response.ContentType = "application/pdf";
context.Response.TransmitFile(context.Server.MapPath(context.Request.FilePath));
context.Response.End();
break;
}
}