Generally you'd want to do something like this; The code supplied here is an
extract, and may require some massaging in your respective projects.
Clientside, set up a formpanel, with appropriate contents:
private class ImgUploadPanel extends VerticalPanel {
FileUpload upload = new FileUpload();
FormPanel form = new FormPanel();
TextBox pidfield = new TextBox();
TextBox typefield = new TextBox();
Button okBtn = new Button("OK");
Label currentFile = new Label();
public void setPid(String projectId) {
pidfield.setText(projectId);
}
public ImgUploadPanel() {
HorizontalPanel current = new HorizontalPanel();
current.add(new Label("Current file:")); // TODO i18n
current.add(currentFile);
currentFile.setText(truncatePath(CookieManager.getCurrentProject().getBackgr
oundImageUrl()));
currentFile.setTitle(CookieManager.getCurrentProject().getBackgroundImageUrl
());
add(form);
add(current);
HorizontalPanel panel = new HorizontalPanel();
panel.setSpacing(5);
form.add(panel);
upload.setName("upload");
upload.setTitle(CookieManager.getCurrentProject().getBackgroundImageUrl());
panel.add(upload);
panel.add(okBtn);
pidfield.setName("pid");
pidfield.setText(CookieManager.getCurrentProject().getID());
pidfield.setVisible(false);
panel.add(pidfield);
typefield.setName("type");
typefield.setText("projectbg");
typefield.setVisible(false);
panel.add(typefield);
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setAction(GWT.getHostPageBaseURL() + "/bgupload");
form.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent e) {
if (upload.getFilename() == null ||
upload.getFilename().equals("")) {
PiriamUtils.feedback(c.feedback_select_file());
e.setCancelled(true);
}
}
public void onSubmitComplete(FormSubmitCompleteEvent e)
{
CookieManager.getCurrentProject().setUseBackgroundImage(true);
CookieManager.getCurrentProject().setBackgroundImageUrl(upload.getFilename()
);
storeData();
}
});
okBtn.addListener(new ButtonListenerAdapter() {
@Override
public void onClick(Button button, EventObject e) {
form.submit();
}
});
}
The most important lines are
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setAction(GWT.getHostPageBaseURL() + "/bgupload");
On the server, set up a servlet (in this case it's running on /bgupload).
This example uses Apache commons fileupload.
ublic class ImportServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private Logger log = Logger.getLogger(ImportServlet.class);
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// Check that we have a file upload request
if (!ServletFileUpload.isMultipartContent(req))
{
log.error("Not a multipart request.");
resp.sendError(400);
return;
}
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(500000); // adjust ?
FileItem uploadfile = null;
String pid = null, type = null;
// Parse the request
try
{
List<FileItem> items = upload.parseRequest(req);
for (FileItem i : items)
{
if (i.isFormField())
{
if (i.getFieldName().equals("pid"))
pid = i.getString();
else if (i.getFieldName().equals("type"))
type = i.getString();
} else
{
uploadfile = i;
}
}
// do stuff with the sent data.
} catch (Exception e)
{
resp.sendError(500);
return;
}
}
}
From: [email protected] [mailto:[email protected]] On Behalf
Of Patrizio De Michele
Sent: 24. juli 2009 09:19
To: [email protected]
Subject: Re: Image doesnt appear when in compiled mode
could you post your solution when done?
i have also the same "problem" to solve.....i simply search possible
solution but
i haven't already developed it...
bye P.
2009/7/24 Patrizio De Michele <[email protected]>
look at fileuploader of gwt-ext-ux (the user extensions)....
ora use the fileupload of gwt com.google.gwt.user.client.ui.fileupload
bye pat
2009/7/24 144_genting <[email protected]>
Hi all n thanks Pat,
I am now trying to upload an image file to server side and saving the
file on server side. does anybody know how to save this imagefile onto
disk at the serverside? thanks in advance
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"GWT-Ext Developer Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/gwt-ext?hl=en
-~----------~----~----~----~------~----~------~--~---