2017-05-22 6 views
0

でファイルをダウンロードするには、私は、このような何かコンテンツとしてストリームファイルにHttpResponseMessageを返して残りのコントローラを持っている:私は、ブラウザのすべてのものにULRでこのメソッドを呼び出すとどのように闊歩UI

public class MyController : ApiController 
{ 
    public HttpResponseMessage GetFile(string id) 
    { 
     try { 
      var stream = fileSystemUtils.GetFileStream(filePath); //Get Stream 
      HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); 
      response.Content = stream; 
      return response; 
     } 
     catch (FileNotFoundException) 
     { 
      return new HttpResponseMessage(HttpStatusCode.NotFound); 
     } 
    } 
} 

はOKと私ですこのファイルをダウンロードできます。今はSwagger UIを使ってダウンロードしたいと思います。これを行う簡単な方法はありますか?

+1

、あなたがあるAJAX要求を使用して、HTTPリクエストを使用していますファイルのダウンロードには適していません。それはできますが、あなたはSwagger UIコードを変更する必要があります –

答えて

1

他の誰かがこのために探している場合は、私は次のようでした:あなたは、そのアドレスに行くとき、あなたが闊歩を使用する場合

public class FileOperation : IOperationFilter 
{ 
    public void Apply(Operation operation, OperationFilterContext context) 
    { 
     if (operation.OperationId.ToLower() == "apifileget") 
     { 
      operation.Produces = new[] { "application/octet-stream" }; 
      operation.Responses["200"].Schema = new Schema { Type = "file", Description = "Download file"}; 
     } 
    } 
} 

//In the startup... 
services.AddSwaggerGen(c => 
{ 
    //missing code... 
    c.OperationFilter<FileOperation>(); 
});