.Net Web API PDF Download not working

I am working on a Durandal application based on the .Net MVC5 project. I have a Web API 2 controller that generates PDF, taking some data and merging it into an existing PDF template using FDFToolkit . This works fine, and I can save the new PDF to disk. My problem is that I want to transfer the new PDF browser to the browser so that the user can download it, but it does not work. I tried many solutions to no avail.

Ideally, I would prefer not to save the generated PDF to disk, as it does not seem necessary. This is the first way I tried to do this - save everything in memory and write a new PDF to an array of bytes and send it back to the browser. After each solution that I could find, I decided to save the new file to disk and try to transfer this new file to the browser. I decided that this would help narrow down the problem.

Here are my response headers after api request

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 169729
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=myEticket.pdf

But nothing happens in the browser (latest versions of Chrome, Firefox, and IE).

Here is my controller and boot method:

[RoutePrefix("api/tickets")]
[AllowAnonymous] // for debugging only
public class EticketsController : ApiController
{
     [HttpGet,HttpPost, Route("download")]
    public HttpResponseMessage DownloadPdf([FromBody] Eticket model)
    {
        const string templateName = "eTicketFinal.pdf";
        const string outputname = "eTicket_new.pdf";

        var templatePdfPath = Path.Combine(HttpContext.Current.Server.MapPath("~/eTickets/"), templateName);
        var outputpath = Path.Combine(HttpContext.Current.Server.MapPath("~/eTickets/"), outputname);

        var eticket = _unitOfWork.EticketRepository.GetById(model.EticketId);

        var pdfticket = Mapper.Map<EticketPdf>(eticket);

        using (var fdfApp = new FDFApp_Class())
        {
            using (var fdfDoc = fdfApp.FDFCreate())
            {
                var properties = pdfticket.GetType().GetProperties();

                foreach (var prop in properties)
                {
                    var name = prop.Name;
                    var propvalue = prop.GetValue(pdfticket, null);
                    var value = propvalue == null ? string.Empty : propvalue.ToString();

                    fdfDoc.FDFSetValue(name, value);
                }

                // this is working and creating the file on disk
                fdfDoc.PDFMergeFDF2File(outputpath, templatePdfPath);                  
                fdfDoc.FDFClose();
            }
        }

        var stream = new FileStream(outputpath, FileMode.Open);

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StreamContent(stream)
        };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        //use attachment to force download
        result.Content.Headers.ContentDisposition = new   ContentDispositionHeaderValue("attachment")
        {
            FileName = "myEticket.pdf"
        };
        return result;

    }
}

Javascript makes an api request:

        $.ajax({
            type: 'POST',
            url: '/api/tickets/download',
            data: postdata,
            datatype: 'json',
            contentType: 'application/json; charset=utf-8'
        });

Durandal/Knockout/Breeze , EF6, Breeze Web API2. , , PDF, , runAllManagedModulesForAllRequests="true" modules web.config.

?

+3
1

. .

, , XHR/ajax . , , <a> HTML href, API. GET.

, GET, . application/pdf, , Chrome, .

+2

All Articles