Prompt file download

前端 未结 5 1853
一生所求
一生所求 2020-12-11 14:39

I have a link on my page on click of which I am trying to generate a PDF document and then show the \"Open - Save\" prompt on the browser.

My HTML (reactjs component

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 15:38

    In our app(angular) we had to create an object url for that with a code like:

    WebApi:

    result = Request.CreateResponse(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(data);
    result.Content.Headers.Add("Content-Type", "application/pdf");
    result.Content.Headers.ContentDisposition =
                            new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                            {
                                FileName = "FileNameHere"
                            };
    
    return result;
    

    javascript:

    // HttpCall in here
    // On SuccessResponse
        var file = new Blob([data], {
                        type: 'application/pdf'
                        });
        var fileURL = URL.createObjectURL(file);
    // create an anchor and click on it.
        var ancorTag = document.createElement('a');
        ancorTag.href = fileURL;ancorTag.target = '_blank';
        ancorTag.download = 'CouponOrder.pdf';
        document.body.appendChild(ancorTag);
        ancorTag.click();
        document.body.removeChild(ancorTag);
    

提交回复
热议问题