Hi ,
I tried to upload some files with FileUpload widget. And also on the
server side I made a Upload handler.
But when i try to upload some file, it does not send any file to the
server side. I dont get any response from the server. Also I noticed
when I changed the service name to something else. I does not give any
error or something.
So I think the FormHandler does not send any data to server side at
all. I tried everything written on this group. But havent found the
solution yet. Can you help me please?
Heres the code:
Client side
// Create a FormPanel and point it at a service.
final FormPanel form = new FormPanel();
form.setAction("/FileUpload");
// Because we're going to add a FileUpload widget, we'll need to
set the
// form to use the POST method, and multipart MIME encoding.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);
// Create a TextBox, giving it a name so that it will be
submitted.
final TextBox tb = new TextBox();
tb.setName("textBoxFormElement");
panel.add(tb);
// Create a FileUpload widget.
final FileUpload fileUpload = new FileUpload();
fileUpload.setName("uploadFormElement");
panel.add(fileUpload);
// Add a 'submit' button.
panel.add(new Button("Submit", new ClickListener() {
public void onClick(Widget sender) {
form.submit();
}
}));
form.addFormHandler(new FormHandler(){
public void onSubmit(FormSubmitEvent event)
{
if (fileUpload.getFilename().length() == 0)
{
Window.alert("You did not specify a script
filename!");
event.setCancelled(true);
}
System.out.println("stopped"); // this is for testing
to see if
it gets here
}
public void onSubmitComplete(FormSubmitCompleteEvent event)
{
Window.alert(event.getResults());
}
});
RootPanel.get().add(panel);
Server Side:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException{
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
FileItem uploadItem = getFileItem(request);
if (uploadItem == null) {
response.getWriter().write("File not uploaded correctly");
return;
}
// save the file to disk
File file = saveFile(uploadItem, getServletContext());
if (file == null) {
response.getWriter().write("File upload error");
return;
}
response.getWriter().write("File uploaded successfully.");
response.flushBuffer();
}
/**
* Return the first FileItem of a HTTPServletRequest
* @param request The HTTPServletRequest for a file upload
operation
* @return The first file of the file upload request
*/
@SuppressWarnings("unchecked")
private FileItem getFileItem(HttpServletRequest request) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items =
upload.parseRequest(request);
Iterator<FileItem> it = items.iterator();
FileItem item = (FileItem) it.next();
return item;
}
catch (FileUploadException e) {
return null;
}
}
/**
* Save a file on a directory of the server.
* @param item file to save
* @param sc Servlet context
* @return error code
*/
public File saveFile(FileItem fileItem, ServletContext sc)
{
try {
File file = new File(sc.getRealPath("/") +
"AN_UNPLOAD_DIR",
fileItem.getName());
fileItem.write(file);
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
And also I put this in the xml file:
<servlet path="/FileUpload"
class="com.socialnetwork.server.FileUploader"/>
What do I wrong?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" 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/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---