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

后端 未结 9 624
灰色年华
灰色年华 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:45

    You need to set the Content-Disposition header on the HttpResponseMessage:

    HttpResponseMessage response = new HttpResponseMessage();
    response.StatusCode = HttpStatusCode.OK;
    response.Content = new StreamContent(result);
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "foo.txt"
    };
    

提交回复
热议问题