0

I have this ajax call that is calling a controller that I wanted to download a file using the HttpResponseMessage. Do I need to do anything with the returns data in the AJAX success call? Should just returning the HttpResponseMessage download the file for the user?

    [HttpGet, HttpPost]
    [ApplicationApiAuthorize("Administrator, ContentManager")]
    public IHttpActionResult PrintTemplates([FromBody] List<string> versionKeys)
    {
    
        var templates = versionKeys
                .Select(v => TemplatesDataService.GetTemplate(v))
                .ToList();
    
        var templateIds = templates.Select(b => b.Id).ToList();
    
        var templateFile = TemplatesDataService.PrintTemplate(templateIds);
    
        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(templateFile.Content)
        };
    
        result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
        {
            FileName = templateFile.FileName
        };
    
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
        var response = ResponseMessage(result);
    
        return response;
    }



   printItems(versionKeys: string[]): JQueryPromise<any> {
        console.log('printItems');

        $.ajax({
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(versionKeys),
            url: this.apiUrls.PrintTemplates,
            success: function (data, status, xhr) {

                console.log('success');
                console.log(data);
              
            }
        });

        return;
    }
}
Moinul Robin
  • 60
  • 1
  • 9
Jefferson
  • 127
  • 9

0 Answers0