上传图片(前端)

问题:

  描述: 前端时间,我用ajaxupload.js写图片上传,出现了后台数据返回不到的情况,后面就用了另外一种方式实现了,主要是因为跨域问题.

解决方案:

  利用iframe框架上传图片

  前端代码:

<form id="img_uploadinp" name="uploadFrom" target='tarframe'  action="{url:/seller/seller_get_photo}" method="post"  enctype="multipart/form-data">
<input class='inp-hid' type="file" name="upfile" onchange="readFile()" accept="image/gif, image/jpeg" title="选择图片">
<input type="hidden" name="postFileName" :value="upfile"/>
</form>
<iframe src="" width="0" height="0" style="display:none;" name="tarframe"></iframe>

js代码:
function readFile() {
$("#i
mg_uploadinp").submit();//表单提交
};
//此方法后台调用
function stopSend(str) {
//str:就是后台返回的图片的url
}

注意要点:
<iframe>标签必要要,并且name属性值和表单target的值要保持一致;

php代码
  //调用js的stopSend方法stopSendstopSend

  echo "<script>parent.stopSend('$name')</script>";

原文地址:https://www.cnblogs.com/fangyinghua/p/fangyinghua.html