J
JaneneM
I've implemented a FileUpload servlet using AJAX and JS. It appears to
be working well but for one issue. I used XMLHTTP so I could intercept
the response in Javascript and write it out to a field on my webpage.
I get back that my readyState is 4 and my status is 200 and status
text is "OK", but my responseText is always empty. Both on FireFox and
IE7, so it must be something I'm doing (wrong). I've also set up an
IFRAME to redirect the results of the POST, and that updates fine, so
the response *is* coming back, but just not the
xmlhttpRequest.responseText. I'm baffled.
Any ideas? TIA!
Janene
Here's my HTML file:
*******
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var httpRequest;
function doUpload( )
{
url = "http://localhost:8022/FileUploadWithHttp/FileUpload.do";
httpRequest=null;
try {
httpRequest=new XMLHttpRequest();
}
// code for IE
catch (e) {
try {
httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support Ajax.");
return false;
}
}
if (httpRequest!=null) {
try {
httpRequest.open('POST',url,true);
} catch (e) {
alert(e);
}
httpRequest.onreadystatechange=state_Change;
httpRequest.send(null);
return true;
}
else
{
alert("Your browser does not support XMLHTTP.");
return false;
}
}
function state_Change()
{
// if xmlhttp shows "complete"
if (httpRequest.readyState==4) {
if (httpRequest.status == 200) {
contents = httpRequest.responseText; // ALWAYS BLANK!!! ARG
alert(contents);
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Select File</title>
</head>
<body>
<form name="myform" method="post" action="http://localhost:8022/
FileUploadWithHttp/FileUpload.do"
enctype="multipart/form-data" onsubmit="javascript:doUpload();"
id="myform" target="fileUploadResultsFrame">
Select your file:<br />
<input type="file" name="myfile"><br /><br />
<input type="submit" name="Submit" value="Submit your file"/>
</form>
File Contents: <br>
<iframe id="fileUploadResultsFrame" src="FileUploadResults.html" ;
scrolling="no"
frameborder="1" height="100" width="50%" longdesc="File Upload
Results"
name="fileUploadResultsFrame" /> </iframe>
</body>
</html>
*******
Here's the little servlet I'm running via Tomcat:
package org.natureserve.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
public class FileUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// first check if the upload request coming in is a multipart
request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// // if not, discard - DO TO something more meaningful
if (!isMultipart) {
//request.setAttribute("msg", "ERROR: Request was not multipart!");
//request.getRequestDispatcher("msg.jsp").forward(request,
response);
return;
}
// // Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//
// // Parse the request
try {
FileItemIterator items = upload.getItemIterator(request);
if (items.hasNext()) {
FileItemStream item = (FileItemStream)items.next();
// check if the current item is a form field or an uploaded file
if (item.isFormField()) {
// skip
} else {
String type = item.getContentType();
if (!type.contains("csv") && !type.contains("excel")
&& !type.contains("comma")) {
request.setAttribute("msg",
"ERROR: Request was not csv!");
request.getRequestDispatcher("msg.jsp").forward(
request, response);
return;
}
String name = item.getFieldName();
BufferedReader stream = new BufferedReader((new
InputStreamReader(
item.openStream())));
response.setContentType("text/html; charset=ISO-8859-1");
String contents = stream.readLine();
PrintWriter out = response.getWriter();
out.print(contents);
out.flush();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
******
And msg.jsp is just this:
<%
String msg = (String)request.getAttribute("msg");
if(msg != null) {
if (msg.contains("ERROR")) {
out.println("<font size=+1>" + msg + "</font><br/>");
out.println("Use back button to select a different file.");
return;
}
}
%>
*****
The iframe file is just a blank html file - it seems to be overwritten
anyway by the results of the request.
be working well but for one issue. I used XMLHTTP so I could intercept
the response in Javascript and write it out to a field on my webpage.
I get back that my readyState is 4 and my status is 200 and status
text is "OK", but my responseText is always empty. Both on FireFox and
IE7, so it must be something I'm doing (wrong). I've also set up an
IFRAME to redirect the results of the POST, and that updates fine, so
the response *is* coming back, but just not the
xmlhttpRequest.responseText. I'm baffled.
Any ideas? TIA!
Janene
Here's my HTML file:
*******
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var httpRequest;
function doUpload( )
{
url = "http://localhost:8022/FileUploadWithHttp/FileUpload.do";
httpRequest=null;
try {
httpRequest=new XMLHttpRequest();
}
// code for IE
catch (e) {
try {
httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support Ajax.");
return false;
}
}
if (httpRequest!=null) {
try {
httpRequest.open('POST',url,true);
} catch (e) {
alert(e);
}
httpRequest.onreadystatechange=state_Change;
httpRequest.send(null);
return true;
}
else
{
alert("Your browser does not support XMLHTTP.");
return false;
}
}
function state_Change()
{
// if xmlhttp shows "complete"
if (httpRequest.readyState==4) {
if (httpRequest.status == 200) {
contents = httpRequest.responseText; // ALWAYS BLANK!!! ARG
alert(contents);
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Select File</title>
</head>
<body>
<form name="myform" method="post" action="http://localhost:8022/
FileUploadWithHttp/FileUpload.do"
enctype="multipart/form-data" onsubmit="javascript:doUpload();"
id="myform" target="fileUploadResultsFrame">
Select your file:<br />
<input type="file" name="myfile"><br /><br />
<input type="submit" name="Submit" value="Submit your file"/>
</form>
File Contents: <br>
<iframe id="fileUploadResultsFrame" src="FileUploadResults.html" ;
scrolling="no"
frameborder="1" height="100" width="50%" longdesc="File Upload
Results"
name="fileUploadResultsFrame" /> </iframe>
</body>
</html>
*******
Here's the little servlet I'm running via Tomcat:
package org.natureserve.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
public class FileUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// first check if the upload request coming in is a multipart
request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// // if not, discard - DO TO something more meaningful
if (!isMultipart) {
//request.setAttribute("msg", "ERROR: Request was not multipart!");
//request.getRequestDispatcher("msg.jsp").forward(request,
response);
return;
}
// // Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//
// // Parse the request
try {
FileItemIterator items = upload.getItemIterator(request);
if (items.hasNext()) {
FileItemStream item = (FileItemStream)items.next();
// check if the current item is a form field or an uploaded file
if (item.isFormField()) {
// skip
} else {
String type = item.getContentType();
if (!type.contains("csv") && !type.contains("excel")
&& !type.contains("comma")) {
request.setAttribute("msg",
"ERROR: Request was not csv!");
request.getRequestDispatcher("msg.jsp").forward(
request, response);
return;
}
String name = item.getFieldName();
BufferedReader stream = new BufferedReader((new
InputStreamReader(
item.openStream())));
response.setContentType("text/html; charset=ISO-8859-1");
String contents = stream.readLine();
PrintWriter out = response.getWriter();
out.print(contents);
out.flush();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
******
And msg.jsp is just this:
<%
String msg = (String)request.getAttribute("msg");
if(msg != null) {
if (msg.contains("ERROR")) {
out.println("<font size=+1>" + msg + "</font><br/>");
out.println("Use back button to select a different file.");
return;
}
}
%>
*****
The iframe file is just a blank html file - it seems to be overwritten
anyway by the results of the request.