www.webdeveloper.com
Results 1 to 3 of 3

Thread: uploading files and text

  1. #1
    Join Date
    Aug 2006
    Location
    Monterrey, Mexico
    Posts
    162

    uploading files and text

    Hello...
    I have a problem.. and i need some help with it..
    this is my form in html:
    Code:
    <form method="POST" name="form2" id="form2" enctype='multipart/form-data' action="/servlet/uploadImage">
    				<input type="file"/>
    			</form>
    and this is the class im using:
    Code:
    public class uploadImage extends HttpServlet {
    
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
            String fileName=processFile(request);
            HttpSession sess = request.getSession();
            if(fileName!=null){
            	sess.setAttribute("nombreLogo",fileName);
            }else{
            	sess.setAttribute("nombreLogo","null");
            }
        }
        
        public String processFile(HttpServletRequest req) {
        	String fileNameReturn="";
            try {
                // object capable of parsing request
                DiskFileUpload fu = new DiskFileUpload();
    
                // max number of bytes
                fu.setSizeMax(1024*100); // 100 K
    
                fu.setSizeThreshold(4096);
    
                //fu.setRepositoryPath("/tmp");
    
                // file processing
                List fileItems = fu.parseRequest(req);
    
                if(fileItems == null)
                {
                    System.out.println("The list is null");
                    return null;
                }
    
                System.out.println("<br>The number of files uploaded is: " +  fileItems.size());
    
                // We iterate on the files
    	    Iterator i = fileItems.iterator();
                FileItem actual = null;
                System.out.println("estamos en la iteración");
    
                while (i.hasNext())
                {
                    actual = (FileItem)i.next();
                    String fileName = actual.getName();
                    System.out.println("<br>File uploaded: " + fileName);
    
                    // we create a file object
                    File fichero = new File(fileName);
                    System.out.println("The file name is: " + fichero.getName());
                    fileNameReturn=fichero.getName();
                    fichero = new  File("d:\\path\\" + fichero.getName());
    
                    actual.write(fichero);
                }
    
            }
            catch(Exception e) {
                System.out.println(e.getMessage());
                return null;
            }
    
            return fileNameReturn;
        }
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		processRequest(request, response);
    
    		HttpSession sess = request.getSession();
    		if(String.valueOf(sess.getAttribute("nombreLogo")).equals("null")){
    			sess.setAttribute("msg","El tamaño del archivo excede el máximo permitido.");
    		}
    			response.sendRedirect("/showroom/personalizaInteractivo.jsp");
    		
    
    	}
    
    
    }
    What i want to do is to be able to put other types of "inputs" such as "text" on the html form, the problem im having is that when the class tries to process the request, it throws an exception because the first input is no "file"

    i dont know if im being clear.. ask me for more info if you think it is necessary

    I need to send something like this
    HTML Code:
    <form method="POST" name="form2" id="form2" enctype='multipart/form-data' action="/servlet/uploadImage>
          Name: <input type="text" name="name"/><br/>
          Upload your picture: <input type="file"/><br/>
          <input type="submit"/>
    </form>
    and i need to insert those values in a database.

    the class i got from a tutorial from here:
    http://www.adictosaltrabajo.com/tuto...ina=fileupload
    its in spanish though..

    thanks in advance

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    you need to differentiate that if a field is a form field or its a file item...
    PHP Code:
    FileItem item = (FileItemiter.next();

        if (
    item.isFormField()) {
            
    //process form field data here
        
    } else {
            
    //process file data here
        

    Hope this helps
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Aug 2006
    Location
    Monterrey, Mexico
    Posts
    162

    thanks!!
    I didnt know that property existed.. that DID work for me..

    thanks again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles