2

I am working on an old ASP.NET WebApi with .Net Framework 4.6.1 and MVC3 project. The problem is that I don't have the File return type, not the one from System.IO but the one from the controller. In ASP.NET Core for example, this File is located in ControllerBase class. So, since my project only knows about System.IO.File, when writing the following code I get the following error:

code:

return File(memory, mimeType, document.Name);

error:

Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of a FileStream objects.
Non-invocable member 'File' cannot be used like a method.
Method, delegate or event is expected.

So is there any way to get this 'File' in my project? Thank you guys!

sayah imad
  • 1,507
  • 3
  • 16
  • 24
  • 1
    Can you tell us how is your controller class defined? – Matteo Umili Jul 28 '20 at 12:37
  • Is https://stackoverflow.com/questions/26038856/how-to-return-a-file-filecontentresult-in-asp-net-webapi what you are looking for? – mjwills Jul 28 '20 at 12:50
  • @MatteoUmili, so my Controller is defined like this : ` [WebApiAuthorize] public class NetworkController : BaseApiController {...}` and that BaseApiController is defined like this: `public abstract class BaseApiController : ApiController {...}` – IoanaPatricia Jul 28 '20 at 15:46
  • @mjwills, thank you for your answer. So yeah, as a workaround, my function has a HttpResponseMessage return type, as in the provided link. However, I prefer going for ActionResult return type, and return a "ControllerBase.File" result. And here comes the problem, ControllerBase.File does not exist. – IoanaPatricia Jul 28 '20 at 15:50
  • @FMR i'am realy sorry i have not understand what is the value that you bring to the post by your comment . – sayah imad Jul 28 '20 at 16:18

1 Answers1

0

You're mixing up your .net API's the File() method is from .net Core, you're using .net framework, so that's why File isn't available. For .net framework you need to use the HttpResponseRessage for .net framework in your controller, something like below:

    byte[] msarray = memory;

    HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
    httpResponseMessage.Content = new ByteArrayContent(msarray);
    httpResponseMessage.Content.Headers.Add("x-filename", document.Name);
    httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
    httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    httpResponseMessage.Content.Headers.ContentDisposition.FileName = filename;
    httpResponseMessage.StatusCode = HttpStatusCode.OK;
    return httpResponseMessage;
munkiepus
  • 66
  • 7