I am having issues with my GWT application being able to upload
images. My html form and PHP file work fine but when I try and upload
an image with my GWT app and my PHP file it does not work and I get a
null response.
I am new to GWT so I am probably overlooking something simple/trivial
Thanks.
This is my code:
package com.poolsupplies.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ListBox;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class LayoutTest implements EntryPoint {
/**
* This is the entry point method.
*/
private FlexTable inputTable= new FlexTable();
private TextBox author = new TextBox();
private TextBox title = new TextBox();
private TextBox desc = new TextBox();
//add the keyword to a list
private TextBox keyword = new TextBox();
private TextBox fileName = new TextBox();
private Button addButton = new Button("Submit");
private ListBox layout = new ListBox();
private FileUpload upload = new FileUpload();
public void onModuleLoad() {
final FormPanel form = new FormPanel();
form.setAction("http://www.myurl.com/phpwrite/upload.php");
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
VerticalPanel vPanel = new VerticalPanel();
form.setWidget(vPanel);
addButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if(title.getText().isEmpty()) {
title.setStyleName("error");
}
else {
title.setStyleName("");
}
if(desc.getText().isEmpty()) {
desc.setStyleName("error");
}
else {
desc.setStyleName("");
}
if(keyword.getText().isEmpty()) {
keyword.setStyleName("error");
}
else {
keyword.setStyleName("");
}
if(fileName.getText().isEmpty()) {
fileName.setStyleName("error");
}
else {
fileName.setStyleName("");
if(!isValidFileName()) {
fileName.setStyleName("error");
}
}
form.submit();
}
});
upload.setName("uploadedfile");
inputTable.setText(0, 0, "Author: ");
inputTable.setWidget(0, 1, author);
inputTable.setText(0, 3, "Tile: ");
inputTable.setWidget(0, 4, title);
inputTable.setText(1, 0, "Description: ");
inputTable.setWidget(1, 1, desc);
inputTable.setText(1, 3, "Keywords: ");
inputTable.setWidget(1, 4, keyword);
inputTable.setText(2, 0, "Filename: ");
inputTable.setWidget(2, 1, fileName);
inputTable.setWidget(2, 4, addButton);
inputTable.setText(3, 0, "Upload File: ");
inputTable.setWidget(3, 1, upload);
vPanel.setWidth("100%");
vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
vPanel.add(inputTable);
form.addFormHandler(new FormHandler() {
public void onSubmitComplete(FormSubmitCompleteEvent event) {
Window.alert(event.getResults());
}
public void onSubmit(FormSubmitEvent event) {
}
});
// Add image and button to the RootPanel
RootPanel.get().add(form);
}
private boolean isValidFileName() {
String fn = fileName.getText();
Window.alert(fn);
if(fn.contains(".")) {
Window.alert("Not a valid file name no extensions required");
return false;
}
else {
return true;
}
}
}
PHP Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
</head>
<body>
<?php
// Where the file is going to be placed
$target_path = "files/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']
['name']);
$target_path = "files/";
$target_path = $target_path . basename( $_FILES['uploadedfile']
['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],
$target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
</body>
</html>
HTML Form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php"
method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /
>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---