On IIS Express, everything works fine. I transfer to the server - when I try to download the file, it writes "Could not download". Although physically it is in the inetpub folder. IUSRS user rights are given full on the inetpub folder

var date = DateTime.Parse(model.Date).ToString("dd.MM.yyyy h:MM:ss"); string newstring = client.HelloBaza(normalUser, date); System.IO.File.WriteAllBytes(Server.MapPath("~/"+normalUser+".xlsx"), Convert.FromBase64String(newstring)); string filepath = Server.MapPath("~/" + normalUser + ".xlsx"); byte[] filedata = System.IO.File.ReadAllBytes(filepath); string contentType = MimeMapping.GetMimeMapping(filepath); var cd = new System.Net.Mime.ContentDisposition { FileName = normalUser + ".xlsx", Inline = true, }; Response.AppendHeader("Content-Disposition", cd.ToString()); return File(filedata, contentType); 
  • one
    I would do so. Stackoverflow.com/questions/660116/ ... Only instead of Transmit - Response.Write(filedata) - nick_n_a
  • Thanks) it helped - Radzhab
  • This method is not an MVC method. What else you need File - I can not say. Perhaps Response.Clear will help. - nick_n_a
  • I did not inspect .... you transfer just what you need. - nick_n_a
  • one
    "when trying to download a file it writes" - who writes something? What headlines are coming back? - Pavel Mayorov

1 answer 1

I will consider several cases.

  • It seems not the first time they ask how to generate a file on the fly, I will give an example

      Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AppendHeader("Content-Disposition", "attachment; filename="+normalUser + ".xlsx"); Response.Write(filedata); // filedata - массив байт Response.End(); 

ContentType list here

  • Often, generators can work with Stream, then the record will be something like this

     DataTable dt = new DataTable(); // Например Response.Clear(); Response.ContentType = "text/xml"; Response.AppendHeader("Content-Disposition", "attachment; filename="+normalUser + ".xml"); dt.WriteXml(Response.OutputStream); Response.End(); 
  • If you need to transfer a file (forward with a substitution of the name or not), then this is done as follows.

  • If you just need to give the go-ahead to download files with a certain extension from the folder, then you just need to add a Copy-handler in web.config

     <configuration> <system.web> <compilation> <buildProviders> <add extension=".xlsx" type="System.Web.Compilation.ForceCopyBuildProvider" /> </buildProviders> 

    Permission can be given both for a specific directory (recommended) and for the entire pool (this is easier).

  • one
    But the direct work with Response for MVC-action is not recommended ... - Pavel Mayorov
  • I agree. The author of the question is missing something in the code. I can not say that. - nick_n_a