file download with custom action

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
marundeo
User
Posts: 10

file download with custom action

Post by marundeo »

Hi,
I would like to download a dynamically generated PDF like in the example below. Anyway the return type is bool thus this dowsn't produce any effect. Some suggestion?

// Row Custom Action event
public bool Row_CustomAction(string action, Dictionary<string, object> row) {
    if (action == "star") { // Check action name
    Syncfusion.Pdf.PdfDocument doc = new Syncfusion.Pdf.PdfDocument();
                //Add a page.
                Syncfusion.Pdf.PdfPage page = doc.Pages.Add();
                Syncfusion.Pdf.Graphics.PdfGraphics graphics = page.Graphics;
                Syncfusion.Pdf.Graphics.PdfFont font = new Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 20);

                graphics.DrawString("Hello World!!!", font, Syncfusion.Pdf.Graphics.PdfBrushes.Black, new PointF(0, 0));
    
                MemoryStream stream = new MemoryStream();
                doc.Save(stream);


                stream.Position = 0;
                //Download the PDF document in the browser.
                FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
                fileStreamResult.FileDownloadName = "Sample.pdf";


                return true;
    }
    

    return true;
}

MichaelG
User
Posts: 1110

Post by MichaelG »

You should add your own API action to return the file:
https://aspnetmaker.dev/docs/#/customsc ... api_action


marundeo
User
Posts: 10

Post by marundeo »

Thank you for the answer.
What is the best approach to consume the custom API from Row_CustomAction?


MichaelG
User
Posts: 1110

Post by MichaelG »

If you prefer to use Row_CustomAction rather than custom API action, you can, but you need to use your fileStreamResult, e.g.

ActionResult = fileStreamResult;


marundeo
User
Posts: 10

Post by marundeo »

For me it is perfectly fine to use custom API action. The question is: how to invoke from ListActions?


MichaelG
User
Posts: 1110

Post by MichaelG »

As explained:

If you prefer to use Row_CustomAction rather than custom API action, you can, but you need to use your fileStreamResult

So the codes in your Row_CustomAction should be like:

ActionResult = ...; // Return file stream result
return true;


marundeo
User
Posts: 10

Post by marundeo »

Thank you, this I was trying already. The issue is that PDF output is getting printed (uncomprehensible text) inside popup. I am not manage to get it downloaded (Config.ActionAjax set in ListActions.Add)


MichaelG
User
Posts: 1110

Post by MichaelG »

If you want to output download file, do not use Ajax for the action, you should use Post Back.


Post Reply