Dear all,
We have started a project to display .PDF or .MP4 video files.
we choused for files stored with in the mounted folder location. instead of storing the SQL Database for eliminate slow response in query run in long run.
We have completed a few steps:
Step 1: We created a "Mount Folder Path" - folder name: "D:\Digital_SOP_Folder"
Step 2: We created a "Master Data screen" using "FileUpload" with an "OnFileReceived" Script:
# --- 1. Get File Information ---
filename = event.file.name
filedata = event.file.getBytes() # <-- UNCOMMENTED THIS
filesize = event.file.size
file_extension = filename.split('.')[-1].lower()
# Determine file type
if file_extension == 'pdf':
file_type = 'PDF'
elif file_extension == 'mp4':
file_type = 'MP4'
else:
file_type = 'Other'
# Update UI components
self.getSibling("File_Name").props.text = filename
self.getSibling("File_Size").props.text = str(round(filesize / 1024.0, 2)) + str(' ~ KB')
# --- 2. Save the File to the Server ---
server_folder_path = "D:\\Digital_SOP_Folder\\"
full_server_path = server_folder_path + filename
try:
system.file.writeFile(full_server_path, filedata)
except Exception as e:
# Handle error - maybe the F: drive is full or permissions are wrong
self.getSibling("Label_1").props.text =("Error saving file: " + str(e))
return
# --- 3. Construct the CORRECT Web Path ---
Web_Path = "/webdev/Digital_SOP_Demo/Digital_SOP_Folder/" + filename
error_message = ''
# --- 4. Write to SQL Database ---
db = "localSQL"
query = """
INSERT INTO StoredFiles (FileName, FileExtension, WebPath)
VALUES (?, ?, ?)
"""
args = [filename, file_type, Web_Path]
try:
system.db.runPrepUpdate(query, args, db)
except Exception as e:
error_message = "Error writing to database: {e}"
self.getSibling("Label_1").props.text = (error_message) # Keep for logging
In this script, we are able to read the file, store it in "D:\Digital_SOP_Folder\", and also store the file information in the SQL database.
Step 3: In the Main screen > Dropdown: we get the file name and file ID from the SQL database. In Button > On Action Performed script:
# Get the ID from dropdown
file_id = self.getSibling("Dropdown").props.value
system.perspective.print("Selected ID: " + str(file_id))
try:
# Query database to get filename from ID
query = "SELECT FileName FROM StoredFiles WHERE FileID = ?"
result = system.db.runPrepQuery(query, [file_id],"localSQL")
if len(result) > 0:
filename = result[0]["FileName"]
system.perspective.print("Filename: " + filename)
# Construct the full file path
file_path = "D:/Digital_SOP_Folder/" + filename
# Read the PDF file as bytes
pdf_bytes = system.file.readFileAsBytes(file_path)
# Convert to base64 string
import base64
pdf_base64_string = base64.b64encode(pdf_bytes).decode('utf-8')
# Create data URI
data_uri = "data:application/pdf;base64,{}".format(pdf_base64_string)
# Set to PDF Viewer
self.getSibling("PDFViewer").props.source = data_uri
system.perspective.print("PDF loaded successfully")
else:
system.perspective.print("No file found for ID: " + str(file_id))
except Exception as e:
system.perspective.print("Error: " + str(e))
Issue: Some files the PDF Viewer displays "Failed to load the Page."
We have checked a few points:
-
The folder path is correct
-
File extension is okay
-
File Size is less then 100KB
-
We also checked the URL of the file in Google Chrome browser and the file opens, but in the Ignition PDF viewer it shows "Failed to load the page"
Questions:
-
Has anyone experienced this issue before?
-
Are there any known limitations with the PDF Viewer component when using base64 data URIs?
-
Could this be related to file size limitations?
-
This approach is better?.
