Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet File Upload is working in FireFox & Chrome but not in IE7 using java & jquery
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Aasif Iqbal
File Upload is working in FireFox & Chrome but not in IE7 using java & jquery
1 Answer(s)      2 years and 10 months ago
Posted in : JSP-Servlet


Hi,

I have a form which is called on my button click "Upload a file" and shows dialogue to select the file.After that I am calling Jquery function to upload that file.In my jquery I am calling a servlet for upload.The code works fine at FireFox & Chrome,but when I tried to upload the file from Internet Explorer I wonder that the file is getting created at my destination folder but the content is still not there.I checked the file size also.It shows me some bytes.I am trying to upload images.

can somebody please help me...I am stuck.....


Advertisement
View Answers

February 24, 2012 at 12:22 PM


1)page.jsp

<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="../UploadServlet" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
<table>
</center>
</FORM>
</HTML>

2)UploadServlet.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class UploadServlet extends HttpServlet{ 
     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
     response.setContentType("text/html");
     PrintWriter out = response.getWriter();

    String saveFile="";
    String contentType = request.getContentType();
    if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
    DataInputStream in = new DataInputStream(request.getInputStream());
    int formDataLength = request.getContentLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    while(totalBytesRead < formDataLength){
    byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
    totalBytesRead += byteRead;
   }
    String file = new String(dataBytes);
    saveFile = file.substring(file.indexOf("filename=\"") + 10);
    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
    saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
    int lastIndex = contentType.lastIndexOf("=");
    String boundary = contentType.substring(lastIndex + 1,contentType.length());
    int pos;
    pos = file.indexOf("filename=\"");
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    int boundaryLocation = file.indexOf(boundary, pos) - 4;
    int startPos = ((file.substring(0, pos)).getBytes()).length;
    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
    File ff = new File(saveFile);
    FileOutputStream fileOut = new FileOutputStream(ff);
    fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();
    out.println("You have successfully upload the file:"+saveFile);
    Connection connection = null;
    String connectionURL = "jdbc:mysql://localhost:3306/test";
    ResultSet rs = null;
    PreparedStatement psmnt = null;
    FileInputStream fis;
    try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "root");
    File f = new File(saveFile);
    psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
    fis = new FileInputStream(f);
    psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
    int s = psmnt.executeUpdate();
    if(s>0){
    System.out.println("Uploaded successfully !");
    }
    else{
    System.out.println("Error!");
    }
    }
    catch(Exception e){
        e.printStackTrace();
        }
    }
  }
}


Related Tutorials/Questions & Answers:
File Upload is working in FireFox & Chrome but not in IE7 using java & jquery
File Upload is working in FireFox & Chrome but not in IE7 using java & jquery  Hi, I have a form which is called on my button click "Upload a file... works fine at FireFox & Chrome,but when I tried to upload the file from Internet
javascript and jquery not working in google chrome and firefox
javascript and jquery not working in google chrome and firefox  <...["textBoxId"] = "q"; settings["callBackFunction"]="callBack"; loadSuggestionControls(settings); function callBack() { settings["q"]=document.getElementById
Advertisements
some methods not working on firefox and chrome
some methods not working on firefox and chrome  why some of javascript methods are not working on firefox and chrome? ex : createElement(). Please help me to solve this problem. adv thanx
Focus method not working in firefox
Focus method not working in firefox  object.focus() is not working in firefox and chrome the element is not focused what was the problem
file upload plugin in jquery
file upload plugin in jquery  file upload plugin in jquery   You can use the following code to use the JQuery plugin. $(document).ready(function() { $("#uploadbutton").jsupload({ action: "addFile.do
html dropdown not working firefox
html dropdown not working firefox  I am writing a Dropdown code in HTML which is not working in firefox. What could be the reason as it's perfectly working in IE and Crome. Thanks
how to upload file using FTP in java
how to upload file using FTP in java  hai, i want to upload file using FTP in java Webservices.. Tel me some Articles Regards Asha
How to save form data to a csv file using jquery or ajax
How to save form data to a csv file using jquery or ajax  Please let... this. i am able to read the csv file using this code if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari data=new
file upload using JSP
file upload using JSP  I have created a form to upload a file...="java" %> <HTML> <HEAD><TITLE>Display file upload form... To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"><
file upload using spring - Spring
file upload using spring  How to upload a file in server using spring framework
How to upload file using JSP?
How to upload file using JSP?   Hi all, I m the beginner in JSP, I want to upload file on server in specific folder.   1)page.jsp... file upload form to the user</TITLE></HEAD> <
Java upload file to ftp
Java upload file to ftp  Hi, How to uploaded file to a ftp server in Java? Thanks   Hi, It's very easy to write a program in Java.... View complete example at FTP File Upload in Java Thanks
Print pdf file in JavaScript - IE and Chrome
;!--Print pdf file in JavaScript - IE and Chrome (if not working in chrome add...Print pdf file in JavaScript - IE and Chrome  <!DOCTYPE html>...{ PDFIframeLoad(); // for chrome } } //for Chrome function
Java For Firefox
Java For Firefox      ... with the java or not USE flag enabled: # eix -e mozilla-firefox... globally to /etc/make.conf. Both cases requires recompiling firefox using
Java FTP file upload example
; Hi, We have many examples of Java FTP file upload. We are using Apache FTPClient for creating FTP based program in Java. See the complete list of FTP...Java FTP file upload example  Where I can find Java FTP file upload
java file upload in struts - Struts
java file upload in struts  i need code for upload and download file using struts in flex.plese help me  Hi Friend, Please visit the following links: http://www.roseindia.net/struts/strutsfileupload.shtml http
file upload download - Java Beginners
file upload download  how to upload and download files from one system to another using java.io.* and java.net.* only please send me code
javascript in firefox - Java Beginners
javascript in firefox  i am not able to run javascript in firfox. firefox does not run javascript. i have also enabled it from tool -> options and checked on enabled javascript still its does not run. it does give any
upload a file and write it in JSP using servlet
upload a file and write it in JSP using servlet  Hello, I'm facing a problem here. I want to upload a file through abc.jsp and write the contents of file using a servlet in xyz.jsp. It is supposed to be a excel file which
Print pdf file in JavaScript - IE and Chrome
Print pdf file in JavaScript - IE and Chrome   <!DOCTYPE html>...{ PDFIframeLoad(); // for chrome } } //for Chrome function...; PRINT PDF IN IE AND CHROME <table> <tr> <td> <
form based file upload using servlets
form based file upload using servlets  Hai all, I wrote a program to upload a file into specified directory and store the file path and username... used commons-fileupload and commons-io APIs. while i enterd username and upload
file upload in jsp - Java Beginners
file upload in jsp  how to upload a file using jsp. my operating...: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload:   2
JQuery-event working new element
JQuery-event working new element  I want to know- why not an event(in JQuery) works on my created element through jQuery
upload and download a file - Java Beginners
upload and download a file  how to upload a file into project folder... the following code: 1)page.jsp: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload
How I Upload File and Store that file name in Database using JSP
How I Upload File and Store that file name in Database using JSP  Hi All, I m the beginner in JSP and I want to upload the file and store that file... form like Emp name, Emp id, city, address and one employee profile image(upload
FTP File Upload in Java
FTP File Upload in Java This tutorial shows you how you can write Java program... easy to write code for FTP File Upload in Java. I am assuming that you have FTP... File Upload in developed Java. You can just change the IP address, username
JQuery-JSP AJAX example not working
working on some additional development code using AJAX and I was getting some...JQuery-JSP AJAX example not working  Hi, I while back ran you tutorial at http://www.roseindia.net/tutorial/jquery/jqSquareroot.html. I copied
File Upload in J2ee on solaris machine using sftp - JSP-Servlet
File Upload in J2ee on solaris machine using sftp  Hi, Currently we are using FTP to upload the file from our J2EE web application... the application in such a way that file upload can be done using SFTP on remote Solaris
how do i upload a file by using servlet or jsp?
how do i upload a file by using servlet or jsp?  hi plz tell me the write java code
file upload in jsp - Java Beginners
file upload in jsp  how to upload a file in jsp and store it in my... += byteRead; } String file = new String(dataBytes); String saveFile...)).getBytes()).length; File f = new File("C:\\"+saveFile); FileOutputStream fileOut
Upload CSV File into Columns of sql table using servlets and jsp
Upload CSV File into Columns of sql table using servlets and jsp  Hello sir, plz give me the code to upload csv file data into respective columns of table
Advertisements
 

 

 

DMCA.com