0

I need to send a file to client to be downloaded using javscript and I have found the following way to do that:

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename="myflie.txt");
Response.ContentType = "text/plain";
Response.Flush();
Response.TransmitFile(filePath);
Response.End();

However I cannot get filename because it seems that this returns a raw file and calling getResponseHeader doesn't work. I'm also having trouble downloading the file itself as there is an error Resource 'blob: …' not allowed to load. Also besides the filename I would also like to pass some other parameters as JSON and I'm unable to do that using this method. I'm a beginner when it comes to asp.net so I hope I was clear enough.

Chen Guevara
  • 324
  • 1
  • 4
  • 14
  • Possible duplicate of [Returning a file to View/Download in ASP.NET MVC](https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Milney Apr 15 '19 at 09:50

1 Answers1

0

You have tagged as using MVC, if so you really don't want to be messing with the Response directly... You can use the File() method:

https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.file?view=aspnet-mvc-5.2

Or look at some of these alternatives to allow changing some metadata:

Returning a file to View/Download in ASP.NET MVC

If you want to download file and send json you will need to use multipart, although I would avoid doing that and set up two different actions instead as this way is more compatible

Milney
  • 6,253
  • 2
  • 19
  • 33