I've implemented an upload utility using Struts2. I'm already restricting uploading a specific file type by programatically checking in my setFileContentType() method in my action class.
One remaining issue is to display to the user a customized error page in case the uploaded file exceeds the max file size setting.
I've researched this and saw how the validation interceptor should be used along with the returned "input" result. However, i still can't put all the pieces together.
My end goal is the following: If the user attempts to upload a large file, i want to display a new page with my own error message.
Any tips/suggestions?
UPDATE
I have the following configuration in my struts.xml:
<action name="FileUpload" class="common.FileUpload">
<interceptor-ref name="fileUpload"/>
<result name="success">common/FileUpload/FileUpload.jsp</result>
<result name="UploadResult">common/FileUpload/FileUploadResult.jsp</result>
</action>
I know the above configuration is missing the validation interceptor in case i want to detect the file size error. The problem is that i'm not sure how that comes into play at this point.
Thanks
FileUpload has a filesize parameter, you can use that in your configuration
<interceptor-ref name="fileUpload">
<param name="maximumSize">50</param>
</interceptor-ref>
If you want to provide custom message you can set here
struts.messages.error.file.too.large
Occurs when the uploaded file is too large as specified by
maximumSize.
Make Your action ValidationAware and you will be notified if Struts2 encounters this error, your addFieldError will be invoked to notify you of the error where key will struts.messages.error.file.too.large and message that you have defined in properties file, once your addFieldError is invoked you can take necessary action.
FileUploadInterceptor while uploading files will also run validation on the file or file type, size and if action implements ValidationAware then it will set validation message in that action by calling addFieldError callback method
Related
If I put a hard-coded value it works fine
<Log PathVariable="C:\myProjectDir\myLogs\log.txt"/>
But if I make a separate variable and replace the hard-code, it does NOT work
<Log PathVariable="[LogLocation]"/>
<Variable Name="LogLocation" bal:Overridable="no" Type="string" Value="C:\myProjectDir\myLogs\log.txt" Persisted="yes"/>
I found something on the lines of <WixVariable Id="WixBundleLog"/> but I don't really know how to use it.
My end goal here is that I have a bootstrapper application and I want to change the location where logs of my msi and exe installation is created
I able able to change a Variable element value using _bootstrapper.Engine.StringVariables["LogLocation"] = "C:\ProjectGorilla\logs\log.txt"
Using the above code in C#, the Variable Id="LogLocation" is changed but that change is not reflected in Log element's PathVariable
So, my question is how can I put a variable in PathVariable attribute of Log element
Thanks in advance :)
According to the documentation, the PathVariable attribute is:
Name of a Variable that will hold the path to the log file. An empty value will cause the variable to not be set. The default is "WixBundleLog".
PathVariable does not specify the path where the (bundle) log will be written. It is a variable that the BA is supposed to read if it wants to copy the file somewhere else. The Burn engine never reads this variable's value so changing it during runtime won't do anything.
The (bundle) log is created (https://github.com/wixtoolset/wix3/blob/58abd6993afba08b39e37b0e76b1790161df9231/src/burn/engine/engine.cpp#L499) before loading the BootstrapperApplication (https://github.com/wixtoolset/wix3/blob/58abd6993afba08b39e37b0e76b1790161df9231/src/burn/engine/engine.cpp#L545). There is no way for the BA to change the path.
The package (MSI, EXE, etc) log locations work the same way today as the bundle log. However since they are created after loading the BA, it's theoretically possible that someone could implement a feature so that the BA can change their path.
I make debug on a website running on cakephp 2 (yes it's old i know)
I have strange errors i cannot resolve.
in log i have:
-Error: [MissingControllerException] Controller class Wp-login.phpController could not be found
-Error: [MissingControllerException] Controller ColonisersController could not be found.
-Error: [MissingActionException] Action ImgController::ui-bg_diagonals-thick_90_eeeeee_40x40.png() could not be found.
-Error: [MissingActionException] Action ImgController::moustique-tigre-default.png() could not be found.
...
I search over all the source code for Colonisers but it is not write even once (also i think ColonisersController is a renammed controller because it is misspelled).
I search over the web for the Wp-login.php and it is a wordpress page, so no link to cakephp at all, also not write anywhere in the source code.
Same story for the missings pngs files. not in the source code.
I try to clear the cache folder on server but problems remains.
I have ghost source code? file are somewhere in another cache ?
any idea are welcomed.
By default all request that do not map to an actual file are being passed over to CakePHP, where the app will try to match the request to a route, and if one is found, finally try to match it to a controller and an action.
You seem to have some rather unspecific routes defined that eat pretty much anything as a possible controller name, hence things are being passed further for searching for a matching a controller and an action, which is where the request flow will end, as no matching controller or action can be found - consequently a MissingControllerException or MissingActionException is being triggered, an error is being logged (by default all exceptions are being logged), and in production mode (debug = 0) the app will respond with a 404 error.
So, no ghosts, no cached files, that's just how things work.
Anybody knows how to upload a file and save it into a database. And it can be viewed in a .jsp page. When the user clicks on it, the user can download it and/or if it is a .doc file (like MS Word .doc, .docx files) it can be viewed online like how facebook implements it.
I am a very very new to uploading files. Please be patient with me. What only I knew is this:
<form>
<input type="file"/>
<input type="submit" value="Upload"/>
</form>
And also, how to limit the file size, and limit only a group of file type like upload only .txt,.doc,.pdf etc files.
From the File Upload Interceptor documentation:
Parameters
maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this is not related to the various properties found in struts.properties. Default to approximately 2MB.
allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.
allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded.
For example, to block all files except png, gif and jpeg under 10 MegaBytes:
<interceptor-ref name="fileUpload">
<param name="maximumSize">
10485760
</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg
</param>
</interceptor-ref>
Read more on File size limit and overall Multipart request size limit.
Be sure to check out the official Struts2 File Upload page too for a complete overview of the subject.
We are trying to use Apache PropertiesConfiguration in our project using JSF and JBoss.
My property file is located inside a package say demo.prop by the name of Prop1.prop
Inside my WAR file the same is present under /WEB-INF/classes/demo/prop/Prop1.prop
Using
FacesContext.getCurrentInstance().getExternalContext().getResource("/WEB-INF/classes/demo/prop/Prop1.prop");
I am able to fetch the property file. So when I try to extract a string from the property file using
PropertiesConfiguration pc1=new PropertiesConfiguration(a);
String s1=(String)pc1.getProperty("User_Name");
I am able to get the proper string. Using set property method I am able to set the property also.
pc1.setProperty("User_Name", "hardcodedString");
But I am not able to save the FILE back to this location. No matter what I do it is not able to save the file using pc1.save.
Can anyone please try to tell me how can I save this file back to its original location so that the changes done in the property file remain as it is.
Modifying the WAR file is a bad idea (for example, some web servers may notice the file
modification an redeploy your app, there may be problems when the server explodes the war on deployment etc.)
I would suggest applying an alternative approach - some possibilities are:
Place the properties in a database table, so they can be easily modified
Use an external properties file for overriding your "Prop1.prop" file settings. You can pull this off for example as follows. Assume that the default property values are bundled in your WAR and the modified values are saved to some other path, which is defined using a system property - let say it's called CONFIG_LOCATION. Now after loading your properties from the bundle you read them also from this external "overrides.prop" file - this overrides your defaults from "Prop1.prop":
PropertiesConfiguration pc1=new PropertiesConfiguration(a);
try(FileReader propReader = new FileReader(System.getenv().get("CONFIG_FILES") +"/overrides.prop"){
pc1.load(propReader);
}
When you need to save changes, you do that to "overrides.prop" - this will save all
the properties, not only the changed ones, but that should have no negative effects.
I have a form with a file input:
<input type="file" id="uploadFile" name="uploadFile" />
I submit the form using the ajaxForm method of the JQuery form plugin.
Then, in the code to handle the post, I read and process the file. I use cfspreadsheet to read the file directly from the file input field:
<cfspreadsheet
action="read"
src="#form.uploadFile#"
sheet="1"
query="spreadsheetData"
headerRow="1"
excludeHeaderRow="true"
>
This all works correctly.
I decided that I want to email the spreadsheet to the administrator as well. I thought I could accomplish this simply with a cfmail tag that includes the following cfmailparam tag:
<cfmail to="myEmailAddress#email.com"
from="fromEmail#email.com"
subject="Upload File" type="HTML">
<cfmailparam file="#form.uploadFile#" />
File processed successfully
</cfmail>
However, this is not working correctly - the email is not sent. What am I doing wrong?
Leigh's solution works well and you have probably already implemented in your code. I thought I'd put my 0.02 cents in about why this is a problem to begin with.
When you upload a file the file is placed in a temp folder location. If you do nothing with the file to put it in a final destination the file is deleted - probably at the end of your request.
Meanwhile the cfmailparam does not actually attach the file at runtime. It leaves it to the spooler process to do that. If you take a look in your ColdFusion installs "mail/spool" directory you will see a file with .cfmail extension. If you can't "catch" one before delivery check your undeliverable folder - there's bound to be a few hanging around in there.
The .cfmail file serves as an instruction to the spooler service which sends the mail. It has a subject, from, to, server address, body etc.
If you attach a file you will see something at the bottom of this file that looks like this:
file: D:\jrun\temp\blah.tmp
file-type: application/octet-stream; name="I am the file you uploaded.tmp"
file-disposition: attachment
remove: false
At runtime CF grabs this file and does what Leigh is suggesting - places it as binary with a mailpart (base64 encoded) into the main body of the message. So what is happening is that by the time the spooler service gets around to attempting to open and attach this file the file is long gone because the request has ended. I also think that the file exists with a ".tmp" extension in this temporary directory - which is obviously not what you want to attach (but that could be a previous version of CF).
To fix it, first use cffile with the "upload" action to put the file in a real (instead of temporary) folder on the disk. Then use cfmailparam to attach the file. NOTE: The "remove" attribute set to yes will cause CF to delete the file once it has successfully sent the mail - which is the effect I believe you are looking for.