zoukankan      html  css  js  c++  java
  • JS将文件像form表单一样提交到后台

    这是很简单。。

    HTML

    <div>
      <input type="file" id="myfile">
      <input type="button" value="上传" onclick="HeadPortraitPicture()">
    </div>

    JS代码

    function HeadPortraitPicture()
    {
      if (document.getElementById('myfile').files[0] != null) {//判断上传的文件是否为空
        var fd = new FormData();
        fd.append("fileToUpload", document.getElementById('myfile').files[0]);//这是获取上传的文件
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/DEMO/SettingCode/Exceltolead?type=doExcel");//要传到后台方法的路径
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);//返回来的数据
        xhr.addEventListener("error", uploadFailed, false);//返回异常
        xhr.addEventListener("abort", uploadCanceled, false);//返回连接异常
        xhr.send(fd);//放入文件发送到后台
      }
    }
    function uploadProgress(evt) {
      if (evt.lengthComputable) {
        //var percentComplete = Math.round(evt.loaded * 100 / evt.total);//可以在这里接收进度条数据
      }
      else {
        alert("无法计算!");
      }
    }
    function uploadComplete(evt) {
      /* 服务器返回数据*/
      var message = evt.target.responseText;//接收返回来的数据
    }

    function uploadFailed(evt) {
      alert("上传出错.");
    }

    function uploadCanceled(evt) {
      alert("上传已由用户或浏览器取消删除连接.");
    }

  • 相关阅读:
    CountDownLatch, CyclicBarrier, Semaphore
    工具类中使用@Autowired失败问题
    可重入锁(递归锁)
    读写锁
    自旋锁
    加入BLOG
    控制字符串的超长部分用省略号表示
    java常见面试题总结
    maven打包不运行test脚本的命令
    DataGrip使用教程
  • 原文地址:https://www.cnblogs.com/HeKaiYue/p/7147752.html
Copyright © 2011-2022 走看看