File upload in Oscript
Ever wondered how a fileupload can be done in the content server?
On yes, you can upload files in a myriad of ways, p.ex by using REST calls, even from Python.
But lets stay traditional, using plain Oscript.
Plain Oscript
This demo method loads the file (found under fieldname) in the folderID FoldeID under the standard enterprise (nodeid 2000). To use that inside the GUI, you need to implement al least one Requesthandler (or put it into smartUI as REST)
function Assoc UploadFile(Record r, Integer FolderID, String fieldName)
Dynamic retNodeID = -1
DAPINODE docNode
// Get the ATTACHMENT_FOLDER node and set some options.
// ParentID = 2000 (Enterprise)
Object prgSession = .PrgSession()
Object session = prgSession.DSession()
DAPINODE nodeFolder= DAPI.GetNodeByID(session.fSession,DAPI.BY_DATAID,FolderID,False )
// Make the node name from the file and eventID. It has to be unique, so check it, and append a number if necessary.
String fname=$WebDoc.WebDocUtils.GetFileUploadName(r,fieldName)
Assoc retUniq
String sTmp=fname
Integer cnt=0
retUniq=$LLIApi.NodeUtil.IsNameUnique(sTmp,nodeFolder)
while !(retUniq.OK)
cnt+=1
sTmp=fname+"_"+Str.ValueToString(cnt)
retUniq=$LLIApi.NodeUtil.IsNameUnique(sTmp,nodeFolder)
end
fname=sTmp
// Upload the document as a new node.
Assoc result = $WebDoc.WebDocUtils.CreateFileUploadDocument(nodeFolder,r,fieldName,fname)
return(result)
end
The
String fname=$WebDoc.WebDocUtils.GetFileUploadName(r,fieldName)
contains the name of the file to upload.
This logic assumes that the filename in the server is unique.
Assoc retUniq
String sTmp=fname
Integer cnt=0
retUniq=$LLIApi.NodeUtil.IsNameUnique(sTmp,nodeFolder)
while !(retUniq.OK)
cnt+=1
sTmp=fname+"_"+Str.ValueToString(cnt)
retUniq=$LLIApi.NodeUtil.IsNameUnique(sTmp,nodeFolder)
end
fname=sTmp
ansd this is the upload command in this nodefolder.
Assoc result = $WebDoc.WebDocUtils.CreateFileUploadDocument(nodeFolder,r,fieldName,fname)