File retrieval from database using WebDev Module

def handleFileUpload(event):
    # Extract file details from the file upload component's custom properties
    fileName = event.file.name
    fileSize = event.file.size
    fileBytes = event.file.getBytes()

    # Set custom properties
    self.custom.fileName = fileName
    self.custom.fileSize = fileSize
    self.custom.fileBytes = fileBytes

    # Create a dictionary with the parameters
    params = {
        "fileName": fileName,
        "fileSize": fileSize,
        "fileBytes": fileBytes  # Adjust if necessary based on your SQL column type
    }

    # Execute the named query to insert the file data
    system.db.runNamedQuery("File Storage/file_storage_query", params)

Welcome to the forum, Samuel. Please see Wiki - how to post code on this forum. You can then edit your question to format correctly and the indentation will show correctly.

Check out pturmel's Blob Serve module - we've used it in production to serve up PDF files stored as blobs in a database and it works flawlessly! This will avoid the additional cost of the WebDev module, if you don't need it for any other reason.

2 Likes

I feel like I'm going to have to bookmark a post that says this so I can keep linking to it, but:
It looks like you're using an LLM (ChatGPT, etc) to help with Ignition coding. Don't do that. They simply don't have the requisite domain "knowledge" to avoid blind alleys. For instance:

    # Set custom properties
    self.custom.fileName = fileName
    self.custom.fileSize = fileSize
    self.custom.fileBytes = fileBytes

This is doing nothing. It is, in fact, actively bad, because it's taking up a huge chunk of gateway and session memory, and burning a ton of CPU cycles, to store a lossy representation of the binary data that you're immediately storing into the database anyways.

You didn't actually ask a question, besides your title, so, if you already have the WebDev module, try looking around on the forums for example code that can help. I can guarantee you it exists. If you've tried to find something and can't get it working, post your code that's not working and what's not working.
If you don't already have the WebDev module, then I'll echo the recommendation of Phil's free module.

3 Likes

I have saved the files in the DB using the query:

# Extract file details from the file upload component's custom properties
fileName = event.file.name
fileSize = event.file.size
fileBytes = event.file.getBytes()


# Create a dictionary with the parameters
params = {
    "fileName": fileName,
    "fileSize": fileSize,
    "fileBytes": fileBytes  # Adjust if necessary based on your SQL column type
}

# Execute the named query to insert the file data
system.db.runNamedQuery("File Upload/file_storage_query", params)

I want to retrieve the file using Web Dev module and display the file onto the PDF viewer component. It will be of great help, if I can get to know how to do this.

Web dev is used to expose an API to the "outside world", which doesn't look like what you're after. There's no reason to use webdev if you want to display things with ignition components.

Tell us more about your use case.

Except with the PDF viewer in Perspective... you need to use the WebDev module or @pturmel blob server: Automation Professionals' Blob Server Module

But as @pascal.fragnoud noted, tell us more about what you need to do. It'll help us help you.

Samuel, you posted this as an additional top level topic instead of replying to your existing threads. I've merged the threads together, but don't do this in the future.