Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 2 Support (Qt 5) › HttpRequest Post and open PDF

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #24505

    Martin

    Good morning,

    I have a code posting some data to a webserver. The server then delivers a PDF to the client (my app). But I don’t know how to open this.

    The response body holds the PDF data. But how can I show it? Do I have to save the data first and then open it? If that is the case how can I do it? Or is there another way?

    Here is my current code:

    // Post some data and retrieve PDF
    HttpRequest.post(url, defect)
        .auth(AppSettings.remoteUser, AppSettings.remotePassword)
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .then(function(res) {
            let responseBody = res.body
    
            console.log("PDF creation OK")
    
            // Open the provided PDF...
    
    })
    .catch(function(err) {
        console.error(err.message)
        console.error(err.response)
    })

    Thank you
    Martin

    #24506

    Alex
    Felgo Team
    #24507

    Martin

    Hi Alex,

    I tried your solution. Problem seems to be that the response body contains binary data. When I write the data to file and try to open it, the PDF is corrupt.

    Can I read the response body to binary data?

    HttpRequest.get("https://www.it-syn.de/wp-content/uploads/2013/03/20150203-Firmenprospekt-it-synergy-GmbH-reduziert.pdf")
    .then(function(res) {
        console.log(res.status);
        console.log(JSON.stringify(res.header, null, 4));
        console.log(JSON.stringify(res.body));
    
        var filePath = fileUtils.storageLocation(FileUtils.AppDataLocation) + "_packages/" + "test.pdf";
        var success = fileUtils.writeFile(filePath, res.body)
        if(fileUtils.existsFile(filePath)) {
            console.log("Open file: " + filePath)
            fileUtils.openFile(filePath)
        }
        else {
            console.log("File doesn't exist: " + filePath)
        }
    })
    .catch(function(err) {
        console.error("Download canceled.")
        console.error(err.message)
    })
    
    Regards,
    Martin

     

    #24508

    Alex
    Felgo Team

    Hi Martin,

    I just applied an improvement to the HttpRequest, to correctly handle PDF binary data. With this patch, your code should work exactly as written now. This will be available with the next update, planned to be released in November still. Is that sufficient for you?

    You could also make use of the DownloadableResources item instead in the meantime, to download a PDF, example is here: https://felgo.com/doc/apps-howto-access-and-store-files/

    Best,
    Alex

    #24509

    Martin

    Hi Alex,

    good to hear. I already use DownloadableResource as workaround. But I will need the post feature soon.

    So I’m looking forward to see new release.

    Thank you
    Martin

    #24522

    Martin

    Hi Alex,

    well, November is over. When can we expect the Update?

    Kind Regards,
    Martin

    #24525

    Alex
    Felgo Team

    Hi Martin,

    it just went live! Let me know if the fix for PDFs works for you 🙂

    Best,
    Alex

Viewing 7 posts - 1 through 7 (of 7 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded