PDA

View Full Version : FileUpload



FireGlow
25 May 2009, 4:19 AM
Hey guys!

I'm wondering how to realise a FileUpload with ExtGWT.

I tried this way:


public class ArchetypeImportForm
extends FormPanel
{
private static EHRflexMessages messages = GWTServiceFactory.getEHRflexMessages();
private Button okButton;

private EHRflexFileUploadField archetypeFile = new EHRflexFileUploadField(messages.ehrflex_file(), false);
private ArchetypeDataModel archetypeDataModel;

/**
* EventListener vor login button. It start's the log in process.
*/
private Listener<ButtonEvent> okButtonListener = new Listener<ButtonEvent>() {
public void handleEvent( ButtonEvent buttonEvent ) {
doUpload();
}
};

public void doUpload()
{
if(this.isValid())
{
//TODO: Fieldbinding would be better, but doesn't work that easily
this.archetypeDataModel.setFilePath((String) archetypeFile.getValue());
Dispatcher.get().dispatch( new EHRflexEvent<ArchetypeDataModel>( EHRflexEventType.ARCHETYPE_IMPORT_SUBMIT, archetypeDataModel) );
}
}
/**
* Creates thelogiform with all needed fields and fielbindings to a empty datamodel
*/
public ArchetypeImportForm() {
this.setLabelWidth( 80 );
this.setHeaderVisible(false);
this.setFieldWidth(400);

archetypeDataModel = new ArchetypeDataModel();

// LoginButton
this.okButton = new Button( messages.ehrflex_doUpload());
this.okButton.addListener( Events.OnClick, okButtonListener );
this.okButton.setIconStyle("icon-upload");


// For Button
FieldSet button = new FieldSet();
button.setLayout( new RowLayout( Style.Orientation.HORIZONTAL ) );
button.setHeight( 32 );
button.setBorders( false );
button.add( this.okButton );


// Adding components
this.add(archetypeFile);
this.add( button );

}
}

The Dispatcher transmits a asynccallback to the ArchetypeGWTServiceImpl#importArchetype:


public class ArchetypeGWTServiceImpl extends EHRflexRemoteServiceServlet
implements ArchetypeGWTService {

private ArchetypeService archetypeService = ArchetypeServiceImpl.getInstance();


/**
* @see ArchetypeGWTService#importArchetype(ArchetypeDataModel, String)
*/
public void importArchetype(ArchetypeDataModel archetype,
String sharedSecret) throws EHRflexException {
System.out.println(archetype.getFilePath());
File archetypeFile = new File(archetype.getFilePath());
this.archetypeService.importArchetype(archetypeFile);
}
}

ArchetypeDataModel contains only the filepath as a String.

It works, but I'm wondering if it will work if the client is not the server? I think it doesn't, but how to realise a FileUpload using GWTServices and without reloading the whole website (if you use submit()) ?

Thanks for helping!

FireGlow
26 May 2009, 5:37 AM
Well....

I've searched a bit through a lot of posts in this forum, but I only found an "old" way to add a File...

is there any real example (with server side) for FileUpload in GXT 1.2 ? It's on the feature list of the release... but the only example is without a serverside handling of these action.

I can imagine how it should work, but for example I don't know how I make a "onSuccess" Listener for a direct call to a servlet?

Hopefully someone can help

FireGlow
26 May 2009, 7:58 AM
So, I found an example for an Servlet to handle a FileUpload:


public class ArchetypeUploadServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private ArchetypeService archetypeService;

public ArchetypeUploadServlet() {
super();

}

public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException {
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

try {
archetypeService = ArchetypeServiceImpl.getInstance();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
items = upload.parseRequest(req);
Iterator it = items.iterator();
File uploadedFile;
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
GregorianCalendar tempDate = new GregorianCalendar();
uploadedFile = new File("D:/server/temp/"
+ tempDate.getTimeInMillis() + ".adl");

item.write(uploadedFile);

User user = (User) req.getSession().getAttribute(
User.USER_LOGGED_IN);
if (user == null) {
throw new AccessControlException(
"Kein User in der Session gefunden.");
}
System.out.println(user);
System.out.println(user.getLogin());
archetypeService.importArchetype(uploadedFile, user);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

I registered it in my web.xml and modul.xml!

My relevant formcode is this:


this.setAction(GWT.getModuleBaseURL() + "ArchetypeUploadServlet");
this.setEncoding(Encoding.MULTIPART);
this.setMethod(Method.POST);

[...]

public void doUpload()
{
if(this.isValid())
{
// //TODO: Fieldbinding would be better, but doesn't work that easily
// this.archetypeDataModel.setFilePath((String) archetypeFile.getValue());
// Dispatcher.get().dispatch( new EHRflexEvent<ArchetypeDataModel>( EHRflexEventType.ARCHETYPE_IMPORT_SUBMIT, this.archetypeDataModel) );
new MessageDialog(this.getAction());
this.submit();
}
}



It works fine so far!

But I'm wondering how I can react on a successful uploading of the file? I don't have any callback or something! Is there a mechanism to react of a successful submit() ?