0

i want to send both files and other data to servlet using ajax so i tried with below code

 $("#upbtn1").click(function(event){ 
            event.preventDefault(); 
            var data1 = new FormData($("#formId1")[0]);

            $.ajax( {
              url: 'Proofs',
              type: 'POST',
              data :data1+"&t1="+$("id").val(),
              processData: false,
              contentType: false,
              dataType:"text",
              success:function(response)
              {  
              }

            }); 
          });

but it gives following Exception

org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is text/plain;charset=UTF-8
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:948)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
at com.iseva.Servlet.Proofs.doPost(Proofs.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1519)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1475)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

if i do it with only sending files it will not give any Exception, but when i send both files and other data its troubling, how to send both data please help me.

raj
  • 19
  • 1
  • 7
  • 1
    `data :data1+"&t1="+$("id").val()`- this is wrong. Did you see any ajax example how to pack data? – Mark Oct 26 '17 at 12:32
  • i have been sending only data for so many days but i did not ever send both at time means packing both files and other data but tried with above code , i searched but did not get solution – raj Oct 26 '17 at 12:35
  • i think when we are sending through ajax , no need to add enctype it will be covered in ajax itself – raj Oct 26 '17 at 12:38
  • 1
    You're appending to the `FormData` object as if it was a string - don't do that. Use `append()` to add information to it: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append – Rory McCrossan Oct 26 '17 at 12:39
  • @Keshpeth OP is uploading through AJAX. Attributes on the `form` are irrelevant. – Rory McCrossan Oct 26 '17 at 12:39
  • Sorry, again that's wrong. You need to set `contentType: false` when uploading a `FormData` object so no content type header is sent, which is required. See the duplicate I marked for more information about how this should be set up in code. – Rory McCrossan Oct 26 '17 at 12:43
  • @Rory McCrossan if ts duplicate the original one should solve my problem but it did not. – raj Oct 26 '17 at 13:03
  • 1
    I added a couple more duplicates for you which popped up after 2 seconds searching – Rory McCrossan Oct 26 '17 at 13:05
  • @raj if the duplicates didn't work for you, you should probably explain to us why exactly. Perhaps you didn't implement it properly, or perhaps your circumstance is fundamentally different in a way which you can describe so that we understand. – ADyson Oct 26 '17 at 14:14

0 Answers0