Question
Sinarmas Multifinance
ID
Last activity: 22 Nov 2016 5:06 EST
Customize Validating FileExtension Attachment
Good day everyone,
I wonder if the attachment from the PEGA can be customize by file extension? From another reference post they said to OOTB Filepath or using pyFileAttachment.
I've already check the Control:FilePath and it used 2 parameters to implements a file browser.
SetEncodingNow - Set to truei if File encoding should be updated immediately and pySetValueFromFileName -- specifies an element to be updated with the selected filename via an onchange event if the element is empty.
So do I need to add another parameter for my input validate file extension or do I need to make Java activity to customize it?
Best Regards,
Jimmy
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Sinarmas Multifinance
ID
Done. Already resolved!
First this is mainly involved Java activity inside class Data-WorkAttach-File named pyUploadFile.
Beware if you not sure what do you want to change, don't change it.
ClipboardPage cpThread = tools.getThread().getThreadPage();
ClipboardPage cpRequestor = tools.getRequestor().getRequestorPage();
ClipboardPage cpProcess = tools.findPage("pxProcess");
String exPath= cpProcess.getString("pxServiceExportPath");
String hiddenFileName= tools.getParamValue("hiddenFileName");
String pyCategory="";java.util.StringTokenizer st = new java.util.StringTokenizer(hiddenFileName,";");
while (st.hasMoreTokens()) {String currentFile =(String)st.nextToken();
if("".equals(currentFile))
continue;
exPath= cpProcess.getString("pxServiceExportPath");
int indexOfSeparator = currentFile.lastIndexOf("\\");
if(indexOfSeparator > 0){
currentFile=currentFile.substring(indexOfSeparator +1);
}
else{
indexOfSeparator = currentFile.lastIndexOf("/");
currentFile=currentFile.substring(indexOfSeparator +1);
}
exPath+=currentFile;
Done. Already resolved!
First this is mainly involved Java activity inside class Data-WorkAttach-File named pyUploadFile.
Beware if you not sure what do you want to change, don't change it.
ClipboardPage cpThread = tools.getThread().getThreadPage();
ClipboardPage cpRequestor = tools.getRequestor().getRequestorPage();
ClipboardPage cpProcess = tools.findPage("pxProcess");
String exPath= cpProcess.getString("pxServiceExportPath");
String hiddenFileName= tools.getParamValue("hiddenFileName");
String pyCategory="";java.util.StringTokenizer st = new java.util.StringTokenizer(hiddenFileName,";");
while (st.hasMoreTokens()) {String currentFile =(String)st.nextToken();
if("".equals(currentFile))
continue;
exPath= cpProcess.getString("pxServiceExportPath");
int indexOfSeparator = currentFile.lastIndexOf("\\");
if(indexOfSeparator > 0){
currentFile=currentFile.substring(indexOfSeparator +1);
}
else{
indexOfSeparator = currentFile.lastIndexOf("/");
currentFile=currentFile.substring(indexOfSeparator +1);
}
exPath+=currentFile;
//Reading Uploaded file data
try
{//String strFileName = cpRequestor.getString("pyFileUpload");
String strFileName = exPath;
cpRequestor.putString("pyFileUpload",exPath);
int lio = exPath.lastIndexOf("/") + 1;
String fName = StringUtils.encodeCrossOSString(exPath.substring(lio, exPath.length()));
PRFile prfUploaded = new PRFile(exPath.substring(0, lio).concat(fName ));
String uploadStatus="";ClipboardProperty pxAttachName=myStepPage.getProperty(".pxAttachName");
if( "".equals(currentFile) )
{
uploadStatus= "Please select a file to upload";}
else if (!prfUploaded.exists()) {prfUploaded = new PRFile(exPath);
if (!prfUploaded .exists())
{
uploadStatus= "File " + currentFile + " doesn't exist";
}} else if (prfUploaded.length() == 0) {
uploadStatus= "File " + currentFile + " doesn't exist or empty";}else if (!prfUploaded.isFile()) {
uploadStatus= "File " + currentFile + " is not a file.";
} else if (!prfUploaded.canRead()) {
uploadStatus= "File " + currentFile + " is unreadable; check the file permissions.";
} else {
java.util.HashMap hmFileInfo = pega_rules_utilities.uploadFile(false);ClipboardPage files = tools.findPage("dragDropFileUpload",false);
ClipboardProperty filesList = files.getProperty("pxResults");ClipboardPage FilePage = tools.createPage("Embed-DragDropFile","UploadedFilePage");
/* If uploadFile is fixed to populate the correct fileName and ext when there are more than one dot in the fileName, We can use the hasmap provided by the uploadFile API */
String file = currentFile; /* BUG-194578 :Do not use the encoded file name /prfUploaded.getName(); */
String filetype = file.substring(file.lastIndexOf('.') == -1 ? file.length() : file.lastIndexOf('.')+1,file.length());//String)(hmFileInfo.get("FileExtension"));
String fileName=file.substring(0,file.lastIndexOf('.')==-1?file.length():file.lastIndexOf('.'));
String pyTruncatedFileName = fileName.length()>59 ? fileName.substring(0,59) : fileName;FilePage.putString("pyFileSource",(String)(hmFileInfo.get("FileData")));
if(filetype.equals("")){
FilePage.putString("pyFileName",pyTruncatedFileName);
}else{
FilePage.putString("pyFileName",pyTruncatedFileName+"."+filetype);
}
FilePage.putString("pyFileMimeType",filetype);
FilePage.putString("pyNote",pyTruncatedFileName);
pyCategory=myStepPage.getString("pyCategory");
if("".equals(pyCategory)){
FilePage.putString("pyCategory","File");
}else{
FilePage.putString("pyCategory",pyCategory);
}
myStepPage.putString("pyNote","");
filesList.add(FilePage);
FilePage.removeFromClipboard();
tools.putParamValue("UploadStatus","good-" + hiddenFileName);
}
if (!uploadStatus.equals("")){
ClipboardPage headerPage = cpRequestor.getPage("pyHTTPResponseHeaders");
headerPage.putString("pxHandler", "");
tools.putParamValue("UploadStatus",uploadStatus);
pxAttachName.addMessage(uploadStatus);
}
}
catch (PRRuntimeException e) {
cpThread.putString("pxHTMLStream", e.toString());
myStepPage.addMessage(e.toString());
}
}
In the Java Activity, Pega make Copies from the selected files into the clipboard as a page list. This will be used in pzMultiFile path control, which triggers this activity to copy the chosen files data as a page list into clipboard.
In my case, I need to make validate within the file attachment type, so in this line:
String filetype = file.substring(file.lastIndexOf('.') == -1 ? file.length() : file.lastIndexOf('.')+1,file.length());//String)(hmFileInfo.get("FileExtension"));
You got the file name also the file type of the extension, just add another validation before this line:
//This is yours validate code (For Example: if ( filetype == '.pdf ' || filetype == ',jpeg '){ )
filesList.add(FilePage);
This will prevent the action of 'attachment file'.
Hope it will help you guys!
Pegasystems Inc.
IN
Hi
Have a look at the control pyWordDocumentFilePath
Specially if you see this lines :
accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document
So basically you have to use accept attribute.
So if you want to restrict user for some specific set of files then you have to create your own custom control and modify the accept attribute accordingly
PT. Sinarmas Multifinance
ID
I already to change pyWordDocumentFilePath as Save as
and change the line :
accept="image/jpeg,image/gif,image/png,application/pdf,image/x-eps"
but still same. If i attach a file .txt extension, there is no validation.
Any idea?
Best regards,,
Iqbal
Accepted Solution
Sinarmas Multifinance
ID
Done. Already resolved!
First this is mainly involved Java activity inside class Data-WorkAttach-File named pyUploadFile.
Beware if you not sure what do you want to change, don't change it.
ClipboardPage cpThread = tools.getThread().getThreadPage();
ClipboardPage cpRequestor = tools.getRequestor().getRequestorPage();
ClipboardPage cpProcess = tools.findPage("pxProcess");
String exPath= cpProcess.getString("pxServiceExportPath");
String hiddenFileName= tools.getParamValue("hiddenFileName");
String pyCategory="";java.util.StringTokenizer st = new java.util.StringTokenizer(hiddenFileName,";");
while (st.hasMoreTokens()) {String currentFile =(String)st.nextToken();
if("".equals(currentFile))
continue;
exPath= cpProcess.getString("pxServiceExportPath");
int indexOfSeparator = currentFile.lastIndexOf("\\");
if(indexOfSeparator > 0){
currentFile=currentFile.substring(indexOfSeparator +1);
}
else{
indexOfSeparator = currentFile.lastIndexOf("/");
currentFile=currentFile.substring(indexOfSeparator +1);
}
exPath+=currentFile;
Done. Already resolved!
First this is mainly involved Java activity inside class Data-WorkAttach-File named pyUploadFile.
Beware if you not sure what do you want to change, don't change it.
ClipboardPage cpThread = tools.getThread().getThreadPage();
ClipboardPage cpRequestor = tools.getRequestor().getRequestorPage();
ClipboardPage cpProcess = tools.findPage("pxProcess");
String exPath= cpProcess.getString("pxServiceExportPath");
String hiddenFileName= tools.getParamValue("hiddenFileName");
String pyCategory="";java.util.StringTokenizer st = new java.util.StringTokenizer(hiddenFileName,";");
while (st.hasMoreTokens()) {String currentFile =(String)st.nextToken();
if("".equals(currentFile))
continue;
exPath= cpProcess.getString("pxServiceExportPath");
int indexOfSeparator = currentFile.lastIndexOf("\\");
if(indexOfSeparator > 0){
currentFile=currentFile.substring(indexOfSeparator +1);
}
else{
indexOfSeparator = currentFile.lastIndexOf("/");
currentFile=currentFile.substring(indexOfSeparator +1);
}
exPath+=currentFile;
//Reading Uploaded file data
try
{//String strFileName = cpRequestor.getString("pyFileUpload");
String strFileName = exPath;
cpRequestor.putString("pyFileUpload",exPath);
int lio = exPath.lastIndexOf("/") + 1;
String fName = StringUtils.encodeCrossOSString(exPath.substring(lio, exPath.length()));
PRFile prfUploaded = new PRFile(exPath.substring(0, lio).concat(fName ));
String uploadStatus="";ClipboardProperty pxAttachName=myStepPage.getProperty(".pxAttachName");
if( "".equals(currentFile) )
{
uploadStatus= "Please select a file to upload";}
else if (!prfUploaded.exists()) {prfUploaded = new PRFile(exPath);
if (!prfUploaded .exists())
{
uploadStatus= "File " + currentFile + " doesn't exist";
}} else if (prfUploaded.length() == 0) {
uploadStatus= "File " + currentFile + " doesn't exist or empty";}else if (!prfUploaded.isFile()) {
uploadStatus= "File " + currentFile + " is not a file.";
} else if (!prfUploaded.canRead()) {
uploadStatus= "File " + currentFile + " is unreadable; check the file permissions.";
} else {
java.util.HashMap hmFileInfo = pega_rules_utilities.uploadFile(false);ClipboardPage files = tools.findPage("dragDropFileUpload",false);
ClipboardProperty filesList = files.getProperty("pxResults");ClipboardPage FilePage = tools.createPage("Embed-DragDropFile","UploadedFilePage");
/* If uploadFile is fixed to populate the correct fileName and ext when there are more than one dot in the fileName, We can use the hasmap provided by the uploadFile API */
String file = currentFile; /* BUG-194578 :Do not use the encoded file name /prfUploaded.getName(); */
String filetype = file.substring(file.lastIndexOf('.') == -1 ? file.length() : file.lastIndexOf('.')+1,file.length());//String)(hmFileInfo.get("FileExtension"));
String fileName=file.substring(0,file.lastIndexOf('.')==-1?file.length():file.lastIndexOf('.'));
String pyTruncatedFileName = fileName.length()>59 ? fileName.substring(0,59) : fileName;FilePage.putString("pyFileSource",(String)(hmFileInfo.get("FileData")));
if(filetype.equals("")){
FilePage.putString("pyFileName",pyTruncatedFileName);
}else{
FilePage.putString("pyFileName",pyTruncatedFileName+"."+filetype);
}
FilePage.putString("pyFileMimeType",filetype);
FilePage.putString("pyNote",pyTruncatedFileName);
pyCategory=myStepPage.getString("pyCategory");
if("".equals(pyCategory)){
FilePage.putString("pyCategory","File");
}else{
FilePage.putString("pyCategory",pyCategory);
}
myStepPage.putString("pyNote","");
filesList.add(FilePage);
FilePage.removeFromClipboard();
tools.putParamValue("UploadStatus","good-" + hiddenFileName);
}
if (!uploadStatus.equals("")){
ClipboardPage headerPage = cpRequestor.getPage("pyHTTPResponseHeaders");
headerPage.putString("pxHandler", "");
tools.putParamValue("UploadStatus",uploadStatus);
pxAttachName.addMessage(uploadStatus);
}
}
catch (PRRuntimeException e) {
cpThread.putString("pxHTMLStream", e.toString());
myStepPage.addMessage(e.toString());
}
}
In the Java Activity, Pega make Copies from the selected files into the clipboard as a page list. This will be used in pzMultiFile path control, which triggers this activity to copy the chosen files data as a page list into clipboard.
In my case, I need to make validate within the file attachment type, so in this line:
String filetype = file.substring(file.lastIndexOf('.') == -1 ? file.length() : file.lastIndexOf('.')+1,file.length());//String)(hmFileInfo.get("FileExtension"));
You got the file name also the file type of the extension, just add another validation before this line:
//This is yours validate code (For Example: if ( filetype == '.pdf ' || filetype == ',jpeg '){ )
filesList.add(FilePage);
This will prevent the action of 'attachment file'.
Hope it will help you guys!
-
Jose Navas Rishabh Yadav
Pegasystems Inc.
IN
Thank you!! We appreciate the update :)