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

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

    If you want to ensure that the file name is properly encoded but also avoid the WebApi HttpResponseMessage you can use the following:

    Response.AddHeader("Content-Disposition", new System.Net.Mime.ContentDisposition("attachment") { FileName = "foo.txt" }.ToString());
    

    You may use either ContentDisposition or ContentDispositionHeaderValue. Calling ToString on an instance of either will do the encoding of file names for you.

提交回复
热议问题