写来做个备份吧:

  1. <html>
  2. <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
  3. <form id="upload" enctype="multipart/form-data" method="post">
  4. <input type="file" name="file" id="pic"/>
  5. <input type="button" value="提交" onclick="uploadPic();"/>
  6. <span class="showUrl"></span>
  7. <img src="" class="showPic" alt="">
  8. </form>
  9. <script>
  10. function uploadPic() {
  11. var form = document.getElementById('upload'),
  12. formData = new FormData(form);
  13. $.ajax({
  14. url:"http://001-bucket.abdc.com/upload",
  15. type:"post",
  16. data:formData,
  17. processData:false,
  18. contentType:false,
  19. success:function(res){
  20. if(res){
  21. alert("上传成功!");
  22. }
  23. console.log(res);
  24. $("#pic").val("");
  25. $(".showUrl").html(res);
  26. $(".showPic").attr("src",res);
  27. },
  28. error:function(err){
  29. alert("网络连接失败,稍后重试",err);
  30. }
  31. })
  32. }
  33. </script>
  34. </html>

分类: web

标签:   jquery