ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[danger] ##### html * 这里注意 要隐藏iframe ~~~ <iframe id="iframe" name="iframe"></iframe> <form id="fm1" enctype="multipart/form-data" method="post" action="/ajaxload/" target="iframe"> <input id="load" type="file" name="k3"> </form> <div id="preview"> </div> ~~~ >[danger] ##### js * 注意几个问题,第一个问题是用change 或者是onchange绑定input,需要改变input的类型,连接地址要加\.由于本身是分割符号,所以双\\ ~~~ $(function () { imgfile(); }) function imgfile() { $('#load').change(function () { document.getElementById('iframe').onload = reloadIframl document.getElementById('fm1').submit() }) } function reloadIframl() { var concent = this.contentWindow.document.body.innerHTML; var obj = JSON.parse(concent); if(obj.status){ var img = document.createElement('img') img.src ="\\"+ obj.data $('#preview').empty().append(img); } } ~~~ >[danger] ##### 后台 * 要注意。文件的属性 都在request.FILES.get('k3') ~~~ def ajaxload(request): if request.method == "GET": return render(request,"img.html") if request.method == "POST": ret = {'status':True,'data':None,'message':None} import os import json print(request.FILES) obj =request.FILES.get('k3') file_path = os.path.join('static', obj.name) f = open(file_path,"wb") for i in obj.chunks(): f.write(i) f.close() ret['data'] = file_path return HttpResponse(json.dumps(ret)) ~~~ ![](https://box.kancloud.cn/760ffa124bd5506ef1f74072f5c5188a_602x296.png)