Results 1 to 6 of 6
  1. #1
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Jsp/Servlet Form with Ajax isMultiPartContent Return False

    multipartcontent method returning false.

    Java Code:
    <%@page import="manhattan.domain.impl.Event"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <html>
    <head>
    	<title>Manhattan Live Performance</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<meta name="viewport"
    		content="width=device-width, initial-scale=1, maximum-scale=1">
    	<link href="assets/css/style.css" rel="stylesheet" type="text/css"
    		media="all" />
    	<link href="assets/3thParty/css/bootstrap.min.css" rel="stylesheet"
    		type="text/css" />
    	<link href="assets/3thParty/css/bootstrap-theme.min.css"
    		rel="stylesheet" type="text/css" />
    	
    	<script src="assets/3thParty/js/jquery-3.2.1.min.js"
    		type="text/javascript"></script>
    	<script src="assets/3thParty/js/bootstrap.min.js" type="text/javascript"></script>
    	<style type="text/css">
    		label {color:white !important}
    	</style>
    </head>
    <body>
    	<div class="header">
    		<jsp:include page="AdminHeader.jsp" />
    	</div>
    		<div class="container">
    			<div class="row">
    				<div class="col-md-6 col-md-offset-3">
    					<form action="AdminPanel/AddBand" method="Post" enctype="multipart/form-data">
    						<div class="form-group">
    							<label  for="bandName">GRUP ADI</label>
    							<input type="text" id="bandName" class="form-control" name="bandName">
    						</div>
    						<div class="form-group">
    							
    							<div class="row">
    							<div class="col-md-4">
    								<label  for="member">GRUP ÜYESİ</label>
    								<input type="text" id="member" class="form-control" name="member">
    								<input type="button" class="btn btn-success" style="float: right;" onClick="addMember();" value="Ekle">
    							</div>
    							<div class="col-md-8">
    								<label  for="bandName">EKLENEN ÜYELER</label>
    								<select class="form-control" name="memberList" id="memberList"></select>
    								<input type="button" class="btn btn-success" style="float: right;" onClick="deleteMember();" value="Çıkar">
    							</div>
    							</div>
    						</div>
    						<div>
    							<jsp:include page="UploadImage.jsp"></jsp:include>
    						</div>
    						<div class="form-group">
    							<label >
    								<input type="checkbox">Haftalık Grup
    							</label>
    						</div>
    						<div class="form-group">
    							<label for="bandInfo">GRUP HAKKINDA</label></br>
    							<textarea  class="form-control" id="editor" rows="6" name="bandInfo"></textarea>
    						</div>
    						<div class="form-group">
    							<input type="submit" id="submit" class="btn-success" value="GRUBU EKLE">
    						</div>
    					</form>
    				</div>
    			</div>
    		</div>
    	<div class="footer">
    		<jsp:include page="../Footer.jsp"></jsp:include>
    	</div>
    	<script type="text/javascript">
    			function addMember() {
    				var opt = document.createElement("option");
    				opt.text = document.getElementById("member").value;
    				opt.value = document.getElementById("member").value;
    				var select = document.getElementById("memberList");
    				select.appendChild(opt);
    			}
    			
    			function deleteMember() {
    				var select = document.getElementById("memberList");
    				select.remove(select.selectedIndex);
    			}
    	</script>
    </body>
    </html>
    UploadImage.jsp

    Java Code:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <form id="uploadForm" enctype="multipart/form-data">
    	<div class="form-group">
    		<label  for="bandPicture">GRUP RESMİ</label>
    		<input type="file" accept="image/x-png,image/gif,image/jpeg" id="bandPicture" class="form-control" name="bandPicture">
    		<input type="button" id="uploadImage" class="btn-success" value="Upload">
    		<label id="uploadResult"></label>
    	</div>
    </form>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#uploadImage').click(function(){
          var form = $('#uploadform');
            $.ajax({
              type: 'Post',
              url: 'UploadImage',
              enctype: 'multipart/form-data',
              data: form.serialize(),
              success: function(result) {
                $('#uploadResult').html(result);
              }
            });
        });
      });
    </script>
    Java Code:
    public class UploadImageController extends HttpServlet {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 6737157694478413704L;
    	
    	// Yüklenecek dizin
    	private static final String UPLOAD_DIRECTORY = "assets/images/upload";
    
    	// Upload ayarları
    	private static final int MEMORY_THRESHOLD 	= 1024 * 1024 * 3; 	// 3MB
    	private static final int MAX_FILE_SIZE 		= 1024 * 1024 * 40; // 40MB
    	private static final int MAX_REQUEST_SIZE	= 1024 * 1024 * 50; // 50MB
    	private static final String UPLOAD_SUCCESS = "Dosya Yüklendi";
    	private static final String NO_FILE = "Lütfen Resim Seçin";
    
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		resp.setContentType("text/plain");
    		resp.setCharacterEncoding("UTF-8");
    		PrintWriter out = resp.getWriter();
    		if(!ServletFileUpload.isMultipartContent(req)) {
    			out.print("enctype = multipart/form-data olmalıdır.");
    			out.flush();
    			return;
    		}
    		
    		DiskFileItemFactory factory = new DiskFileItemFactory();
    		factory.setSizeThreshold(MEMORY_THRESHOLD);
    		File repository = new File(System.getProperty("java.io.tmpdir"));
    		System.out.println(repository.getAbsolutePath());
    		factory.setRepository(repository);
    		ServletFileUpload upload = new ServletFileUpload(factory);
    		upload.setFileSizeMax(MAX_FILE_SIZE);
    		upload.setSizeMax(MAX_REQUEST_SIZE);
    		String uploadPath = getServletContext().getRealPath("")
                    + File.separator + UPLOAD_DIRECTORY;
    		System.out.println(uploadPath);
    		
    		try {
    			List<FileItem> images = upload.parseRequest(req);
    			if (images != null && images.size() > 0) {
    				for (FileItem image : images) {
    					if (!image.isFormField()) {
                            String fileName = new File(image.getName()).getName() + System.currentTimeMillis();
                            String filePath = uploadPath + File.separator + fileName;
                            File storeFile = new File(filePath);
                            //Dosyayı Kaydet
                            image.write(storeFile);
                            out.print(UPLOAD_SUCCESS);
                        }
    					else out.print("Form elemanı değil");
    				}
    			} else out.print(NO_FILE);
    		} catch (Exception e) {
    			out.print("Exeption");
    			e.printStackTrace();
    		}
    	}
    	
    		
    
    }

  2. #2
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: Jsp/Servlet Form with Ajax isMultiPartContent Return False

    Anyone help?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Jsp/Servlet Form with Ajax isMultiPartContent Return False

    Don't look at the JSP.
    Look at the HTML that the client (ie the browser) receives, and post the form here.
    It looks to me like you've written a form within a form.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: Jsp/Servlet Form with Ajax isMultiPartContent Return False

    Yes there 2 form. Is that problem for html? Main form for save entity. Sub for for update image. But sub form, value is "Update" button not "submit" type button.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Jsp/Servlet Form with Ajax isMultiPartContent Return False

    So that's intentional?
    Why?

    The outer form is already a multipart one.

    Have you debugged it on the client?
    Have you traced what it is attempting to send (if anything)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: Jsp/Servlet Form with Ajax isMultiPartContent Return False

    Problem is inner form.
    1- I have Band entity. Band entiti have band picture. I want to save band. I have form. But i want to upload picture with ajax, for sure upload is ok. Then send form data. Do you guys have something like that form examle?

Similar Threads

  1. Replies: 37
    Last Post: 07-28-2015, 01:32 PM
  2. Replies: 3
    Last Post: 09-09-2012, 05:35 PM
  3. Need ajax implementation of servlet?
    By alok_sharma in forum Java Servlet
    Replies: 0
    Last Post: 11-19-2010, 12:04 PM
  4. Replies: 0
    Last Post: 10-31-2009, 10:24 AM
  5. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 05:06 PM

Tags for this Thread

Posting Permissions

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