python檔案上傳的三種方式
阿新 • • 發佈:2018-12-27
def upload(request): return render(request, 'upload.html') def upload_file(request): username = request.POST.get('username') fafafa = request.FILES.get('fafafa') with open(fafafa.name, 'wb') as f: for item in fafafa.chunks(): f.write(item) print(username) retviews.py= {'code': '123456', 'data': 'hahahaha'} import json return HttpResponse(json.dumps(ret))
1.xmlHttpResquest
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style>View Code.file{ width: 100px; height: 50px; position: absolute; z-index: 100; opacity: 0; } .upload{ width: 100px; height: 50px; position: absolute; z-index: 90; top: 0; bottom: 0; right: 0; left: 0; background-color: blue; text-align: center; line-height: 50px; border-radius: 30px; color: white; } </style> <body> <div style="position: relative;height: 50px;width: 100px;"> <input class="file" type="file" id="fafafa"> <a class="upload">上傳</a> </div> <input type="button" value="xhr提交" onclick="xhrSubmit()"> <script src="/static/jquery-1.12.4.js"></script> <script> function xhrSubmit() { var file_obj = document.getElementById('fafafa').files[0]; var fd = new FormData(); fd.append('username','root') fd.append('fafafa',file_obj) var xhr = new XMLHttpRequest(); xhr.open('POST', '/upload_file/',true); <!--onreadystatechange可以監測xhr的狀態變化--> xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ // 接收完畢 var obj = JSON.parse(xhr.responseText); console.log(obj); } }; xhr.send(fd); } </script> </body> </html>
2.jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> .file{ width: 100px; height: 50px; position: absolute; z-index: 100; opacity: 0; } .upload{ width: 100px; height: 50px; position: absolute; z-index: 90; top: 0; bottom: 0; right: 0; left: 0; background-color: blue; text-align: center; line-height: 50px; border-radius: 30px; color: white; } </style> <body> <div style="position: relative;height: 50px;width: 100px;"> <input class="file" type="file" id="fafafa"> <a class="upload">上傳</a> </div> <input type="button" value="jQuery提交" onclick="jqSubmit()"> <script src="/static/jquery-1.12.4.js"></script> <script> function jqSubmit() { {#var file_obj = $('fafafa').files[0];#} var file_obj = document.getElementById('fafafa').files[0]; var fd = new FormData(); fd.append('username','root'); fd.append('fafafa',file_obj); $.ajax({ url: '/upload_file/', type: 'post', data: fd, processData: false, // tell jQuery not to process the data contentType: false, // tell jQuery not to set contentType success:function (arg,a1,a2) { console.log(arg); console.log(a1); console.log(a2); } }) } </script> </body> </html>View Code
3.iframe
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/upload_file/" method="POST" enctype="multipart/form-data" target="ifm1"> <!--這裡新增iframe是由於需要有一個html接收"/upload_file/"方法返回的引數,一般為HttpResponse,所以這裡使用iframe接收, iframe一般設定為"display:none"將其隱藏--> <iframe id='ifm1' name="ifm1"></iframe> <input type="file" name="fafafa"> <input type="submit" onclick="iframeSubmit()" value="iframe提交"> </form> <script src="/static/jquery-1.12.4.js"></script> <script> function iframeSubmit() { $('#ifm1').load(function () { var text = $('#ifm1').contents().find('body').text(); var obj = JSON.parse(text) console.log(obj) }) } </script> </body> </html>View Code