在input框中輸入數據,點擊提交按鈕。數據顯示在iframe中,這裏是經過form標籤中的targets屬性來綁定iframe標籤python
def test(request): if request.method == 'GET': return render(request,'test.html') root = request.POST.get('root') ret = {'status':True,'message':root} return JsonResponse(ret)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <h3>基於iframe+Form表單提交數據</h3> <iframe id="iframe" name="ifr"></iframe> <form id="fm" action="/test/" method="post" target="ifr"> <input type="text" name="root"> <a onclick="AjaxSubmit()">提交</a> </form> </div> </body> <script src="/static/js/jquery-3.4.1.min.js"></script> <script> function AjaxSubmit() { document.getElementById('iframe').onload = reloadIframe // iframe的onload屬性,在iframe內容加載時自動執行 document.getElementById('fm').submit(); } function reloadIframe() { var content = this.contentWindow.document.body.innerText // 獲取到iframe內部的值,注意是一個頁面,經過這種方式取值 var obj = JSON.parse(content) if (obj.status){ alert(obj.message) } } </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <h3>上傳文件</h3> <input type="file" id="file"> <button onclick="AjaxSubmit()">提交</button> </div> </body> <script src="/static/js/jquery-3.4.1.min.js"></script> <script> function AjaxSubmit() { var formdata = new FormData(); var file = document.getElementById('file').files[0] formdata.append('file', file) $.ajax({ url: '/test/', method: 'POST', data: formdata, contentType: false, processData: false, success: function (data) { console.log(data) } }) } </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <h3>基於iframe+Form表單上傳文件</h3> <iframe id="iframe" name="ifr" style="display: none;"></iframe> <form id="fm" action="/test/" method="post" enctype="multipart/form-data" target="ifr"> <input type="file" name="file"> <a onclick="AjaxSubmit1()">提交</a> </form> </div> </body> <script src="/static/js/jquery-3.4.1.min.js"></script> <script> function AjaxSubmit1() { document.getElementById('iframe').onload = reloadIframe // iframe的onload屬性,在iframe內容加載時自動執行 document.getElementById('fm').submit(); } function reloadIframe() { var content = this.contentWindow.document.body.innerText // 獲取到iframe內部的值,注意是一個頁面,經過這種方式取值 console.log(content) } </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <h3>基於iframe+Form表單上傳文件</h3> <iframe id="iframe" name="ifr" style="display: none;"></iframe> <form id="fm" action="/upload_img.html/" method="post" enctype="multipart/form-data" target="ifr"> <input type="file" name="file" onchange="AjaxSubmit1()"> </form> <h3>預覽</h3> <div id="preview"> </div> </div> </body> <script src="/static/js/jquery-3.4.1.min.js"></script> <script> function AjaxSubmit1() { document.getElementById('iframe').onload = reloadIframe document.getElementById('fm').submit(); } function reloadIframe() { var content = this.contentWindow.document.body.innerText var obj = JSON.parse(content) var tag = document.createElement('img'); tag.src = obj.data; $('#preview').empty().append(tag); } </script> </html>
def upload_img(request): ret = {'status':True,'data':None,'message':None} obj = request.FILES.get('file') import uuid nid = str(uuid.uuid4()) file_path = os.path.join('static', nid+obj.name) f = open(file_path,'wb') for line in obj.chunks(): f.write(line) f.close() ret['data'] = file_path return HttpResponse(json.dumps(ret))