0

Suppose I have controller which returns file (docx for example, but format doesn't matter, same behavior with jpg, png, etc)

[ApiController]
[Route("[controller]")]
public class FileController : Controller
{
    [HttpGet("")]
    public IActionResult Index()
    {
        var fileName = "File.docx";
        var filePath = @$"C:\Sources\{fileName}";

        FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);

        return File(stream, "application/octet-stream", fileName);
    }
}

If I use iOS Safari I get download dialog: enter image description here

But in case of iOS Chrome, it opens content of the file and places it on the webpage: enter image description here

In case of Chrome on Android tables, it works correctly: enter image description here

Is it possible to fix this behavior for iOS Chrome somehow?

To understand the context of the problem: I place such a link on some web page. And when user clicks on it, iOS Chrome replaces this page with content of a file instead of downloading this file.

PS Btw, when I try the same but using some unknown binary file, iOS Chrome starts to work correctly:

enter image description here

Don Tomato
  • 3,311
  • 3
  • 30
  • 48
  • I think there is something wrong with you content type. For .dox files you should use "application/vnd.openxmlformats-officedocument.wordprocessingml.document" – habib Jun 08 '22 at 06:56
  • @habib Just have tried with same result. I don't think this is a reason. I also tried jpeg with correct content type, with same result. – Don Tomato Jun 08 '22 at 07:01
  • yes, then it may be browser-specific behaviour. I will try it myself also. – habib Jun 08 '22 at 07:03

1 Answers1

0

Your code looks fine but your contentType is not looking right to me.

Please try with application/vnd.openxmlformats-officedocument.wordprocessingml.document

[ApiController]
[Route("[controller]")]
public class FileController : Controller
{
    [HttpGet("")]
    public IActionResult Index()
    {
        var fileName = "File.docx";
        var filePath = @$"C:\Sources\{fileName}";

        FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);

        return File(stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", fileName);
    }
}

For more information about contentType please read this popular answer.

habib
  • 2,366
  • 5
  • 25
  • 41