Saving file to server in ColdFusion - file-upload

After doing some research I feel this should work, however it is not saving the file to my Images directory.
<cfform name="uploadImgForm" method="post" action="#CGI.PATH_INFO#?#CGI.QUERY_STRING#" enctype="multipart/form-data">
<input name="txtImg" type="file" />
<input name="btnSubmit" type="submit" />
</cfform>
<cfif isDefined("Form.txtImg")>
<cffile action="upload"
fileField = "txtImg"
destination="/Images"
accept="image/jpeg"
nameconflict="makeunique">
</cfif>
I plan on doing some validation, but I would like to get this simple example working first.
I came across this later which is helpful when trying to rename a file before upload:
Adobe link

The destination has to be a full path, otherwise it gets sent to a directory relative to the temp directory of ColdFusion.
Try this:
<cfset destination = expandPath("Images") />
<cffile action="upload"
fileField = "txtImg"
destination="#destination#"
accept="image/jpeg"
nameconflict="makeunique">

Are there any error messages at all? Or, after posting, do you just get the form again?
Firstly, does the /Images directory exist under the default CF temp directory?
Secondly, try getting rid of the "accept" argument to the CFFILE, just in case your browser's sending an odd MIME type.

Related

Store blob as file

I am trying to save blob from a form as a file on my server without success:
<form method="post" name="myform" action="action.php">
<input type="hidden" name="urlAddress" id="mytext">
</form>
my mytext.value is set to some window.URL.createObjectURL(data);
Now, I am able to download or view this value from browser: console.log(mytext.value) gives 'blob:https://127.0.0.2/e2f169ab-6165-4466-880e-30642e8c8770'
From PHPside, I use $url=$_POST['urlAddress'];
Questions :
1/ how to save that file (which I am actually able to save from browser!) with php to server disk?
2/ is it possible to use phpmailer to directly send this blob as attached file?
Thanks a lot for any help,
--Fred

Image is not displaying in cfdocument pdf for coldfusion 10

I am using ColdFusion 10 Enterprise edition, and am unable to display images when using CFDOCUMENT to generate a PDF. Below is the piece of code I am using:
<cfsavecontent variable="report">
<table align='center'>
<cfoutput query="VARIABLES.result">
<tr>
<td>
<div class='addInfoDetails'>#SHOWINFO#</div>
</td>
</tr>
</cfoutput>
</table>
</cfsavecontent>
In the above code, the VARIABLES.result query is coming from database and SHOWINFO is a variable having the content of image and text. For example:
SHOWINFO =
"<p> Hi This is the test information
<img alt="image" src="../TestBank/test/info/5KQ.jpg"/>
</p>"
Here if dump the SHOWINFO variable inside the CFSAVECONTENT, the image displays correctly. But when I convert this into PDF, using CFDOCUMENT, the image is not displaying.
Below is the code block I am using to generate the pdf:
<cfdocument format="PDF" saveasname="TestPDF">
<cfoutput>#report#</cfoutput>
</cfdocument>
Thanks in advance.
I fixed it by adding the attribute "localUrl = yes" in CFDOCUMENT tag.
Now it is working fine for me.
I'm saving the created pdf and find localUrl="yes" (or =true) fails. Turns out CF generating pdfs for https urls is painfully finicky.
<img src="file:\\\#replace(getCurrentTemplatePath(),"my.cfm")#images\my.png">
Worked for me, in case anyone is scraping the bottom of the barrel for ideas. getCurrentTemplatePath() gets the filename as well as the path, so I had to remove it (hence the replace(...,"my.cfm"). I also tried expandPath(".") and that failed as well.
Didn't try Dave Anderson's intriguing suggestion to grab the image Coldfusion CFDOCUMENT creates a red X

Can file names be changed on uploading with cffile?

I am currently trying to create a page where bands can upload their own logos onto the site for use where it is needed. Currently, I have created something which allows a user to upload/delete a logo to the allocated directory. But what I want to do is rather than create a band_logo field, have it so the band logo file name becomes the band's id in the database. With this being unique it means I don't have the long-winded process of creating a field to save their logo name. I know there is a cffile action="rename" option but that is a more long-winded process of doing things.
Yes, if you provide the file name in the destination it will rename the file on upload.
<cffile action="upload" destination="/path/to/some/directory/#session.bandName#.jpg" ... />
See Renaming Files As They Are Uploaded (how CFFILE actually works)
BTW, searching google for "name file on cffile upload" found that article...
However, you still may want to use
<cffile action = "upload" ...>
then
<cffile action = "rename" ...>
Because unless you snag the file extension on the client side and pass it on to the server, hard coding the file extension could cause problems if they don't upload a .jpg or whatever extension you designate. It isn't that much code or overhead and it's safer.
<cfif #ServerFileExt# EQ 'jpg'>
<cffile action="upload" destination="/path/to/some/directory/#session.bandName#.jpg" />
<cffile action="rename"
source="path/to/some/directory/#session.bandName#.jpg"
destination="path/to/some/directory/#getData.bandsID#.jpg">
<cfelse>
<cflocation addtoken="no" url="back to form upload page">
</cfif>

ColdFusion CFPDFFORM does not populate PDF form fields

I created a quick PDF file with two form fields, first name and last name (or FirstName and LastName), with a Submit button and used the following code to try to populate these fields in a new PDF file:
<cfpdfform action="populate" source="#variables.AbsPath#\test.pdf">
<cfpdfformparam name="FirstName" value="foo" />
<cfpdfformparam name="LastName" value="bar" />
</cfpdfform>
However, I receive this error:
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.
Null Pointers are another name for undefined values.
Platform, Locale, and Platform Name must not be null
I tried reading the PDF file to make sure the form fields were correct using this and they are:
<cfpdfform action="read" source="#variables.AbsPath#\test.pdf" result="fields" />
<cfdump var="#fields#">
The struct dumps out:
FirstName: [empty string]
LastName: [empty string]
Submit: [empty string]
I tried searching the Adobe docs and around some other sites, but can't find a solution for this error. I also tried adding a "destination" parameter to the cfpdfform tag in case it was needed, but got the same result. The PDF file does exist. Any ideas? Thanks in advance.
UPDATED:
Here is the stack trace:
java.lang.NullPointerException: Platform, Locale, and Platform Name must not be null
at com.adobe.fontengine.fontmanagement.platform.PlatformFontDescription.<init>(Unknown Source)
at com.adobe.fontengine.font.opentype.OpenTypeFont.getPlatformFontDescription(Unknown Source)
at com.adobe.fontengine.font.FontImpl.getPlatformFontDescription(Unknown Source)
at com.adobe.fontengine.font.FontImpl.getPlatformFontDescription(Unknown Source)
at com.adobe.fontengine.fontmanagement.platform.PlatformFontResolverImpl.addFont(Unknown Source)
at com.adobe.internal.pdftoolkit.core.fontset.impl.PDFFontSetImpl.addFont(Unknown Source)
at com.adobe.internal.pdfm.util.FontSetBuilder.loadFontsPath(FontSetBuilder.java:418)
at com.adobe.internal.pdfm.util.FontSetBuilder.loadSystemFonts(FontSetBuilder.java:346)
at com.adobe.internal.pdfm.util.FontSetBuilder.makePDFFontSet(FontSetBuilder.java:239)
at com.adobe.internal.pdfm.util.FontSetBuilder.getPdfFontSet(FontSetBuilder.java:93)
at com.adobe.internal.pdfm.PDFM7Factory.getPdfFontSet(PDFM7Factory.java:97)
at coldfusion.document.DocumentServiceImpl.getAssemblerFontSet(DocumentServiceImpl.java:512)
at coldfusion.pdf.PDFForm.populateFields(PDFForm.java:292)
at coldfusion.tagext.lang.PDFFormTag.doEndTag(PDFFormTag.java:347)
at cfsubmit2ecfm1224790315.runPage(C:\ColdFusion\wwwroot\nVision\projects\test\pdfform\submit.cfm:9)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:231)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:416)
at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:381)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:94)
at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:126)
at coldfusion.CfmServlet.service(CfmServlet.java:200)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
at jrun.servlet.FilterChain.service(FilterChain.java:101)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
I believe this is a font issue with the specific server being used. This is why the same code will work on the production server and not the testing server. I am experiencing the same issue and although I don't have a great solution here's what I learned.
The stacktrace shows that there's an error getting the getPlatformFontDescription of an OpenType font.
Our Windows server is listing some TrueType fonts as OpenType. Is yours?
Using the following code corrected the issue, but it's a work around only.
<cfpdf name="local.pdffile" action="read" source="some path" >
<cfscript>
local.pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(tobinary(local.pdffile));
local.outputStream = createObject("java", "java.io.ByteArrayOutputStream").init();
local.pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(local.pdfReader,local.outputStream);
local.Acroform = local.pdfStamper.getAcroFields();
//Populating Form Fields
local.Acroform.setField("Field1",Arguments.Value1);
local.Acroform.setField("Field2",Arguments.Value2);
// etc.
local.pdfStamper.setFormFlattening(true); //optional
local.pdfStamper.close();
local.pdfReader.close();
local.pdffile = local.outputStream.toByteArray();
</cfscript>
<!--- flatten="no" must be set or you will get the error again --->
<cfpdf action="write" source="local.pdffile" destination="#variables.OutputPath##local.UUID#.pdf" overwrite="yes" flatten="no" />
More Diagnostics: Simply listing all the fonts available to the server using the Java subsystem also fails. Try this.
<cfset list=createobject("java","com.adobe.internal.pdfm.util.FontSetBuilder")>
<cfdump var="#list#">
<cfset dummy = list.getPdfFontSet()>
<cfdump var="#dummy.toString()#">
<!--- this should fail --->
We have "fixed" our problem at the moment by removing all the fonts from the Windows\Fonts folder, and isolating the damaged ones. It's painstaking but seems to work.
Good luck!
You did not mention how you created your PDF file. Depending on how you created the PDF file the form fields may be contained within a subform. PDFs generated from templates within LiveCycle will do this. By default LiveCycle will name the subform "form1". In order to populate these form fields you need to include the cfpdfsubform tag. See the documentation here: cfpdfsubform documentation
So your code would look something like this:
<cfpdfform action="populate" source="#variables.AbsPath#\test.pdf">
<cfpdfsubform name="form1">
<cfpdfformparam name="FirstName" value="foo" />
<cfpdfformparam name="LastName" value="bar" />
</cfpdfsubform>
</cfpdfform>
The code that you included to read your PDF file should show you if the fields are included in a subform. The dump would show your form fields contained within another structure. That would be the name that you use in the cfpdfsubform tag's name attribute.
HTH.
I just wrote up a script to fix this issue here: https://stackoverflow.com/a/20408559/1223555
It's an issue with a font file not having a locale or platform name set. the PDF functions in Coldfusion call this
<cfset list=createobject("java","com.adobe.internal.pdfm.util.FontSetBuilder")>
<cfset dummy = list.getPdfFontSet()>
This enumerates every font and if one is bad it throws an exception and kills your process.
My code enumerates all the fonts in c:\windows\fonts and then tells you the bad ones.
Here are the ones we have issues with:
AdobeNaskh-Medium.otf
Mechanical.TTF
msgothic.ttf
MyriadArabic-Bold.otf
MyriadArabic-BoldIt.otf
MyriadArabic-It.otf
MyriadArabic-Regular.otf
I just realized after re-reading your question that I was going down the wrong path. I was thinking you were having an issue populating the fields in the pdf. But your real issue is the null pointer. Doh! Sorry about the confusion.
Usually that error is telling you that a variable you are using is not defined. The only variable I see in your example is #variables.AbsPath#. You are specifying the variables scope. Is that correct? The variables scope is available only on the page in which it was created (and to included pages).
You did not include how you are defining that variable. Can you share that?
Is the error happening randomly or all the time?
Are you sure the error is happening on that cfpdfform line and not somewhere else? I would put a cftry/cfcatch block around your code and dump the cfcatch structure to get a more specific error. Can you try that and post back?

Max number of file in Fileupload Primefaces

I set my FileUpload component with this params:
<p:fileUpload label="Select image"
mode="advanced"
update="messages"
fileUploadListener="#{fileUploadController.handleFileUpload}"
sizeLimit="1000000"
fileLimit="1"
fileLimitMessage="Only 1 image"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
But if i select more image i don't get any error or warning.
I reproduced the same behavior, but I dont think it's intentional. You can do a little workaround if you use auto="true" because the file will be uploaded immediately. On the other hand you can make sure that your backing bean is just processing the last file uploaded (maybe the user selected a wrong file in the first place and wants to use another one?).
fileLimit="1" only limits the file limit when using mode="advanced" and multiple="true" (see [1]), but that is currently not supported by primefaces (see [2]).
[1] http://forum.primefaces.org/viewtopic.php?f=3&t=14194
[2] https://www.primefaces.org/docs/guide/primefaces_user_guide_6_1.pdf at page 218
I faced the same problem. To solve this i tried update="#this"
fileLimit attribute is also not working in my case. I tried with mode="advanced" and multiple="true". In my case I want to limit the file to be uploaded upto 3. But it is not restricting me to upload more files.