ajax上传文件

不用form表单:

来源:https://www.cnblogs.com/ilovepan/p/10549236.html

不支持$this->request->file()获取图片

后台接收文件请使用$_FILE

html:

<div class="upload">
  <div class="box-upload">
    <span>上传图片</span>
  </div>
  <input type="file" accept="image/*" name="img" class="uploadImg" value="">
  <img src="" alt="" id="imgimg">
</div>

JQUERY:

$(".uploadImg").on("change",function () {

    var formData = new FormData();
    formData.append('img',this.files[0])


    $.ajax({
        url:'/module/controller/uploadImg',
        data:formData,
        contentType: false,
        processData: false,
        type:'post',
        dataType:'json',
        success:function (res) {
            console.log(res)
        }
    })
})

如果你要现实页面上(还没有上传到服务器,但是谦容性有点问题)

function phone(a){
            var txt=a.files[0];
            var src=window.URL.createObjectURL(txt);
            return src;
        }

这样就能在没有上传到服务器上之前拿到图片了

用form表单:

来源:https://www.jb51.net/article/142708.htm

和:https://blog.csdn.net/x550392236/article/details/76690927

posted @ 2019-07-26 14:11  1O(∩_∩)O1  阅读(163)  评论(0编辑  收藏  举报