Grails multi-part form server 500 error on null object when deployed, but not on local host - grails

In my grails project I have a multipart-form that grabs the file and assigns the filename to a variable that gets stored in the database.
When I run the app from localhost with production flag I am able to successfully submit the form, however, after I deploy the app to a glassfish server, I get a server 500 error:
Cannot get property 'originalFilename' on null object
I am thinking that maybe it has something to do with not handling multipart correctly. Anyone have this issue or can point me in the right direction to solving it?
Using Grails 2.0.4 deployed to Glassfish 3 server using Oracle db.
pertinent code from my action:
def uploadedFile = request.getFile('filepath')// see if there is a file to upload
if (!uploadedFile?.empty) { // is there a file?
sampleInstance.filepath = "file://///FileLocation/${uploadedFile?.originalFilename}" // save the original filename
}
Form on gsp:
<g:form action="sample" enctype="multipart/form-data">
<g:textField name="name" value="${sampleInstance?.name}"/>
...
<input type="file" id="filepath" name="filepath" />
<g:submitButton name="submit" value="Submit" /></td>
</g:form>

If uploadedFile is null, then uploadedFile?.empty will be null, so
if (!uploadedFile?.empty) { // is there a file?
Will do the opposite of what you are expecting
You should probably be doing
if ( uploadedFile && !uploadedFile.empty) { // is there a file?

Related

MODX Revolution and Formit plugin: not sending mail attachments

Using MODX Revolution and Formit, I try to attach a file to a contact form. The mail is sent but without the attachment. For some strange reason, I can also no longer find any information on the attachments hook. Has that feature been removed from Formit recently?
[[!FormIt?
&hooks=`spam,email,attachments,redirect`
&emailTpl=`sometemplate`
&emailTo=`foo#foo.com`
&emailBCC=`foo2#foo.com`
&emailSubject=`some subject`
&redirectTo=`123`
&validate=`name:required, filedata:required`
]]
<form action="[[~[[*id]]]]" method="post" class="form" enctype="multipart/form-data">
<input type="text" name="name" id="name" value="[[!+fi.name]]">
<input id="filedata" name="filedata" type="file" value="[[+fi.filedata]]">
<button type="submit">SEND</button>
</form>
Again, mail is sent (I tried many different email adresses) but the attachment is always missing. What's wrong?
Did you try https://modx.com/extras/package/ajaxupload2 extra?
From description:
With two FormIt hooks the upload queue could be pre filled from a FormIt field
value and be saved into a FormIt field value. With a third FormIt hook the
uploaded files could be attached to the FormIt mails.
Just leave the hook 'attachements' away, formit handles a file input without that hook.

How to upload file to FTP Server in grails

I've read the answers to the question asked here :
How to upload file to remote FTP Server in grails
everything compiles and runs without errors. Now how do I reference the upload service from a Grails form? Sorry for the basic question.
So far I'm playing around with
<g:form action="do_something" enctype="multipart/form-data" useToken="true">
<span class="button">
<input type="file" name="thefile"/>
<input type="submit" class="upload" value="upload"/>
</span>
</g:form>
but I just need a few pointers as to how to link this with the service, presumably via the controller.
Work with the request. Get the file name and create a new file. my own code:
def f = request.getFile('myfile');
def webrootDir = servletContext.getRealPath("/"); //app directory
File fileDest = new File(webrootDir,"xmls/");
def xmlFile = new File(fileDest, "settings.xml");
f.transferTo(xmlFile);
Just look at this post for more info.

File upload to ASP. NET MVC 6 not working

The file upload component from a 3rd party vendor does not work with my MVC 6 project. Therefore I built a very simple upload mechanism with standard asp.net components:
<form method="post" asp-action="Index2" asp-controller="Data" enctype="multipart/form-data">
<input type="file" name="files" multiple />
<input type="submit" value="Upload" />
</form>
This upload works fine. I receive the uploaded file in my POST-Method in the controller. However if I start the full featured upload component (dxFileUploader from DevExpress) I don't receive the file. My method in the controller will be called but the file collection is empty. In order to compare the two upload requests I created a Fiddler for both. The requests are very similar. Has someone an idea what's the problematic difference between the two requests?
#Marco, I know this is old, however, ensure that the binding in your controller is correct, meaning the parameter to your action matches the name of the component. I am using the dxFileUploader (version 16.1) with the following action:
public async Task<IActionResult> UploadProducts([FromForm]IFormFileCollection files){...}
And the following in my view :
$("#file-uploader").dxFileUploader({
selectButtonText: "Select Product File",
labelText: "",
accept: "text/csv",
uploadMode: "useForm",
name: "files"
});
I hope this helps.

Send file to server in grails 3.0.0.1

I'm doing some test with grails 3.0.0.M1 and I have a form that sends a file to server using :
In view:
<g:uploadForm controller="file" action="upload">
<input type="file" name="file" />
<input type="submit" value="send"/>
</g:uploadForm>
and In the controller I have:
def file() {
def f = request.getFile("file")
//....some code
}
With f I have a NullPointerException that is to say the server don't receive the file from the view.
How how I can fix it?
Thanks!
There were some issues in Grails 3.0.0 M1 regarding file uploads, please try 3.0.0 RC2

File upload & Spring Security

Based on the Spring Security documentation, I setup a MultipartFileter as the following:
#Order(1)
public class SecurityWebAppInitializer
extends AbstractSecurityWebApplicationInitializer {
#Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
insertFilters(servletContext, new MultipartFilter());
}
In a file upload form, I can see a CSRF input with a not-null value in a HTML file (see the code below).
<form method="POST" enctype="multipart/form-data" action="/upload">
File to upload: <input type="file" name="file" /><br />
Name: <input type="text" name="name" /><br /> <br />
<input type="submit" value="Upload" />
Press here to upload the file!
<input type="hidden" name="_csrf" value="df94be7d-675d-428c-89e5-2ebf0b473c42" />
</form>
After submitting the form, I get an error as
HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
type Status report
message Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
description Access to the specified resource has been forbidden.
What is missing here?
This problem is resolved after changing the Java configuration of the application. The followings are those changes.
In AbstractAnnotationConfigDispatcherServletInitializer class, I add the MultipartFilter in the getServletFilters method and set the MultipartConfig with a MultipartConfigElement in customizeRegistration(ServletRegistration.Dynamic registration) method. A MutlipartConfigElement originally defined in WebMvcConfigurerAdapter class is removed. And a MultipartResolver defined in the class is unchanged.
The Java configuration was set up based on the original XML configuration of the application. The approach doesn't always work based on this case.

Resources