How to set downloading file name in ASP.NET Web API

后端 未结 9 603
灰色年华
灰色年华 2020-12-02 06:11

In my ApiController class, I have following method to download a file created by server.

public HttpResponseMessage Get(int id)
{
    try
    {
        strin         


        
9条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 06:35

    If you are using ASP.NET Core MVC, the answers above are ever so slightly altered...

    In my action method (which returns async Task) I add the line (anywhere before the return statement):

    Response.Headers.Add("Content-Disposition", $"attachment; filename={myFileName}");
    

提交回复
热议问题