Syntax Rommel
Syntax Rommel

Reputation: 942

Failed to download file using file stream

I am able to view the sample image if I simply display to img element, I can also stream it using attribute but the file type is unknown when the download is done.

Here my controller part:

public IActionResult DownLoad(string param)
  {
      string webRootPath = _hostingEnvironment.WebRootPath;
      webRootPath = "//myServ01/folderA";
      var addrUrl = webRootPath + param;      
      var stream = System.IO.File.OpenRead(addrUrl);
      return File(stream, "application/force-download", param);
  }

And this is the html call to display in img and the clickable attribute to download:

<img src="/File/Download?param=sample.png"><br>
<a id="download_link" href="/File/Download?param=sample.png">Download File</a>

I can open the downloaded file but need to manual open paint or image editor, here is the dummy snapshot of the image this just sample file name from my actual file:

enter image description here

Upvotes: 0

Views: 391

Answers (1)

Reyan Chougle
Reyan Chougle

Reputation: 5331

application/force-download is not a standard MIME type. Use the correct mime type for whatever media you are using (e.g. audio/mpeg for mp3).

You can try specifying the generic application/octet-stream MIME type as well.

To get the MIME types you can also use the static class System.Net.Mime.MediaTypeNames. E.g. to use generic MIME type you can use System.Net.Mime.MediaTypeNames.Application.Octet.

Upvotes: 2

Related Questions