如何实现ajax上传

发布于 11-08 18:48 字数 5671 浏览 817 评论 3

function button_upload_onClick(){
      var file_name=document.form.file_name.value;
      var file = document.getElementById('file_name');
     
      if(file_name=="")
       {
            alert("请选择要导入的子项信息文件!");
            document.form.file_name.focus();
           return false;
         }
       var xmlhttp;
       if (window.XMLHttpRequest) {
           xmlhttp = new XMLHttpRequest();
       }
       else {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
       xmlhttp.onreadystatechange = function(){
           if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
             //do....
           }
       }
      
       xmlhttp.open("post", "wl_t_m_instr_req.jsp?excute_flag=true", true);
       xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");
       //var formData = new FormData();
       //formData.append("file", file);
      
       xmlhttp.send(file);

 

 

错误信息如下 :

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
 at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:327)
 at org.apache.jsp.wl.wl_005ft_005fm_005finstr_005freq_jsp._jspService(wl_005ft_005fm_005finstr_005freq_jsp.java:241)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at com.baosight.management.servlet.SecurityLinstener.doFilter(SecurityLinstener.java:50)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at com.bstek.dorado.core.FilterHandle.c(Unknown Source)
 at com.bstek.dorado.core.FilterHandle.doFilter(Unknown Source)
 at com.bstek.dorado.core.DoradoFilter.doFilter(Unknown Source)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at com.baosight.epass2.filter.servlet.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:32)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at com.bstek.dorado.core.GZIPEncodeFilter.doFilter(Unknown Source)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
 at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
 at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
 at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
 at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
 at java.lang.Thread.run(Thread.java:595)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉2021-11-08 18:58:25


jquery.form 就支持文件上传的

疾风者2021-11-08 18:58:19

js一般是不允许读取本地文件的。

一般通过form,让浏览器自动组装,把form的target指向隐藏的iframe,在返回时,放一些js,运行window.parent.fileuploadResult('xxx')完成上传结果的处理。

最简单就是看哪个网站有文件上传,参考下代码。

终遇你2021-11-08 18:56:25

网上很多例子的,  ajax 是不能上传文件的, 都是通过隐藏的 iframe 提交。

可以参考 jquery.form.js 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文