PDA

View Full Version : File upload fails in Web mode in Google Chrome but works in IE6 - Please help



cnestor
5 Nov 2009, 1:34 PM
I can successfully upload a file in hosted mode using the following code. When I compile and try in web mode, it works fine in IE6 but doesn't in google chrome. The form is submitted but it never reaches the POST method in my servlet.
It doesn't work for any browser when I deploy to google app engine.
I don't have any error messages in my log.
The form fires the Events.BeforeSubmit in any cases.
I am also using a GET method in the same servlet to monitor the progress from the client side using the ProgressListener class. This works also fine.
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "/upload");

What am I doing wrong, it's driving me crazy?

Thanks a lot.




...
formPanel = new formPanel ();
formPanel.setHeaderVisible(false);
formPanel.setFrame(true);
formPanel.setAction("/upload");
formPanel.setEncoding(Encoding.MULTIPART);
formPanel.setMethod(Method.POST);
FileUploadField file = new FileUploadField();
...


Web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/trspring-config.xml /WEB-INF/trspring-Security-config.xml
</param-value>
</context-param>
<!-- Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Servlets -->
<servlet>
<servlet-name>DataSource</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>DataSource</servlet-name>
<url-pattern>*.rpc</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>com.tr.gxt.aoi.server.IO.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>AOIDashboard.html</welcome-file>
</welcome-file-list>


</web-app>
Servlet
public FileUploadServlet() {
super();

}


public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException {
logger.info("Server side - GET - FileUploadServlet");
HttpSession session = request.getSession();
logger.info("GET - Session: " +session.getId() + " - " +session.toString());
FileUploadListenerFix f = new FileUploadListenerFix();
FileUploadListener listener = (FileUploadListener) session.getAttribute("progress");

if (listener != null) {
StringBuilder msg = new StringBuilder();
msg.append(formatSize(listener.getBytesRead()));
msg.append("/");
msg.append(formatSize(listener.getContentLength()));
logger.info("message: " + msg);
try {
resp.getOutputStream().write(msg.toString().getBytes());
resp.getOutputStream().close();
} catch (IOException e) {
logger.error(e.getMessage());;
}
}
}

private String formatSize(long bytes) {
long kBytes = bytes / 1024;

if (kBytes > 1024) {
return kBytes / 1024 + "Mb";
} else {
return kBytes + "kb";
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
logger.info("Server side - POST - FileUploadServlet");

if(ServletFileUpload.isMultipartContent(request)) {
HttpSession session = request.getSession();
FileUploadListener listener = new FileUploadListener();
session.setAttribute("progress", listener);
ServletFileUpload upload = new ServletFileUpload();
upload.setProgressListener(listener);

fileDAO = new FileDAOImpl();
String userId = "Admin";

try {
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {

FileItemStream item = iterator.next();
if (item.isFormField()==true) {
if (item.getName()!=null&&item.getName().equals("userId")) {
userId = item.getFieldName();
}
}
if (item.isFormField()==false) {
String str = item.getName();
System.out.println("item: " + item.getName());
//Create new datastore object
TRFile file = new TRFile();
//Load filename
char[] chars = str.toCharArray();
int index = 0;
int index1 = 0;
for (; index < str.length(); index++) {
if (chars[index] == '\\')
index1 = index;
}
String fileName;
if (index1==0) {
fileName = str.substring(index1+1);
}else {
fileName = str.substring(index1+1);
}

file.setFileName(fileName);

//Load opportunityReport flag
if (item.getFieldName().indexOf("Opportunity") > -1) {
file.setOpportunityReport(true);
} else {
file.setOpportunityReport(false);
}

//Load file content
InputStream stream = item.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
int maxFileSize = 1048576; //1 Mb max
if (out.size() > maxFileSize) {
System.out.println("File is > than "
+ maxFileSize);
return;
}
file.setFile(new Blob(out.toByteArray()));

file.setOwner(userId);
file.setContentType(item.getContentType());
//Add object to the datastore for Google App Engine(GAE) support using JDO framework.
fileDAO.add(file);
resp.getWriter().print(file.getId());

}

}
} catch (FileUploadException e1) {
System.out.println(e1.getMessage());
}
}
}

takayser
6 Nov 2009, 2:21 AM
try this:


FileUploadField uploadField = new FileUploadField();
uploadField.setId("upload");
uploadField.setName("upload");

cnestor
6 Nov 2009, 6:40 AM
Thanks Takayser, but it didn't work.
I am using GWT 1.7.1 and GXT.2.0.1.

I applied the fix suggested by micgala in the following thread http://www.extjs.com/forum/showthread.php?t=79188&highlight=file+upload&page=5 to fix the additional window opening and everything works great in hosted mode but the FormPanel/FileUploadField does not submit anything (GET OR POST ) in web mode (compiled).

I am running the TCP/IP monitor in Eclipse but it doesn't trace anything when running Google Chrome since there is no request generated I believe. It only works with IE6. (Haven't tried IE7, IE8 yet). Here is my trace when running IE6 in web mode (does work when deployed to Google app engine however):

POST /upload HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost:8081/AOIDashboard.html
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=---------------------------7d93b71190400
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MS-RTC LM 8)
Host: localhost:8080
Content-Length: 48270
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=i82lk1qyqjsb


Anything hint is welcome, way to debug, etc..., I really need to get that working asap.

Thanks a lot

ITB
7 Dec 2009, 9:22 AM
Hi cnestor,

I have the same problem but only with Chrome and Opera (with IE8 & FF3.5 is ok).

Did you resolve the problem on chrome ?

Thanks

I'm using GTW 1.7.0 and GXT 2.1.0

cnestor
7 Dec 2009, 9:52 AM
I gave up on using ext-GWT for file uploading and used gwtupload instead. Works fine out of the box. See http://code.google.com/p/gwtupload/.

Hope this help, but if you find the answer please let us know.