Hi - I have spent 8 days following tutorials and blogs and tried everything. I have this code below...but the command object in the onSubmit method is always null and I don't know why the file isn't being sent by Extjs... Please help
I am using extjs 3 :
Code:
createFileUpload = function()
{
var URL = "filePreview.do";
myuploadform= new Ext.FormPanel({
fileUpload: true,
width: 250,
autoHeight: true,
bodyStyle: 'padding: 10px 10px 10px 10px;',
labelWidth: 50,
items:[
{
xtype: 'fileuploadfield',
id: 'filedata',
emptyText: 'Select a document to upload...',
fieldLabel: 'File',
buttonText: 'Browse'
}],
buttons: [{
text: 'Preview',
handler: function(){
if(myuploadform.getForm().isValid()){
form_action=1;
myuploadform.getForm().submit({
url: URL,
waitMsg: 'Uploading file...',
headers: {'Content-type':'multipart/form-data'},
success: function(form,action){
alert('Success', 'Processed file on the server');
},
failure: function() { alert("this failed too."); }
});
}
}
}]
})
return myuploadform;
}
I have a large form of ~50 items and I am trying to add this Extjs file upload field, with a "preview" button to open the file for verification (requested by the customer). So far I am just trying to save the file I select; I have not implemented the GET part of the preview yet.
I am using Java 6 with Spring 2.0.8.
Bean #1 :
Code:
package beans;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import org.springframework.web.multipart.MultipartFile;
public class FilePreviewBean
{
private MultipartFile file;
public void setFile( MultipartFile file )
{
this.file = file;
}
public MultipartFile getFile()
{
return file;
}
}
The controller bean :
Code:
package controller;
public class FilePreviewController extends SimpleFormController
{
public FilePreviewController()
{
}
protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors ) throws ServletException, IOException
{
// cast the bean
FilePreviewBean fp = (FilePreviewBean)command;
MultipartFile file = fp.getFile();
if( file == null ) // <------------- IT'S ALWAYS NULL ?!
{
System.out.println("The file was null.");
return null;
}
else
{
// print/return an error
return null;
}
}
protected void initBinder( HttpServletRequest request, ServletRequestDataBinder binder ) throws ServletException
{
//binder.registerCustomEditor( byte[].class, new ByteArrayMultipartFileEditor() );
binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
}
}
And the bean definitions :
Code:
<bean id="multiPartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="filePreviewController" class="controller.FilePreviewController">
<property name="commandClass" value="cbeans.FilePreviewBean"/>
<property name="formView" value="fileuploadform"/>
</bean>
I can see that the bean is called from my preview button. The setFile() method should apparently be called automatically by Spring MVC but when I call getFile() it always returns null.
I was trying to follow this : http://static.springsource.org/sprin...#mvc-multipart
and it makes a lot of sense; but I have no idea what to do to make the file not equal null!? Please help