浏览目录
Form表单上传文件
Ajax上传文件
伪造Ajax上传文件
Form表单上传文件
html
1 2 3 4 5 6 7 | < h3 >form表单上传文件</ h3 > < form action="/upload_file/" method="post" enctype="multipart/form-data">
< p >< input type="file" name="upload_file_form"></ p >
< input type="submit"> </ form > |
注意:必须添加enctype="multipart/form-data属性。
view
1 2 3 4 5 6 7 8 9 | def index(request):
return render(request, "index.html" ) def upload_file(request):
print ( "FILES:" ,request.FILES)
print ( "POST:" ,request.POST)
return HttpResponse( "上传成功!" ) |
Ajax上传文件
什么是FormData
XMLHttpRequest Level 2添加了一个新的接口FormData
.利用FormData对象
,我们可以通过JavaScript用一些键值对来模拟一系列表单控件,我们还可以使用XMLHttpRequest的send()
方法来异步的提交这个"表单".比起普通的ajax,使用FormData
的最大优点就是我们可以异步上传一个二进制文件.
所有主流浏览器的较新版本都已经支持这个对象了,比如Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。
html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | < h3 >Ajax上传文件</ h3 > < p >< input type="text" name="username" placeholder="username"></ p > < p >< input type="file" name="upload_file_ajax" ></ p > < button >提交</ button > {#注意button标签不要用在form表单中使用#} < script >
$("#upload_button").click(function(){
var username=$("#username").val();
var upload_file=$("#upload_file_ajax")[0].files[0];
var formData=new FormData();
formData.append("username",username);
formData.append("upload_file_ajax",upload_file);
$.ajax({
url:"/upload_file/",
type:"POST",
data:formData,
contentType:false,
processData:false,
success:function(){
alert("上传成功!")
}
});
}) </ script > |
views.py
1 2 3 4 5 6 7 8 9 | def index(request):
return render(request, "index.html" )
def upload_file(request):
print ( "FILES:" ,request.FILES)
print ( "POST:" ,request.POST)
return HttpResponse( "上传成功!" ) |
伪造Ajax上传文件
iframe标签
<iframe> 标签规定一个内联框架。
一个内联框架被用来在当前 HTML 文档中嵌入另一个文档。
示例:
1 | < iframe src="http://www.baidu.com" width="1000px" height="600px"></ iframe >< em style=" font-family: ‘PingFang SC‘, ‘Helvetica Neue‘, sans-serif; "> </ em > |
iframe+form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | < h3 >伪造Ajax上传文件</ h3 > < form action="/upload_file/" method="post" target="ifr" enctype="multipart/form-data">
< p >
< iframe name="ifr" ></ iframe ></ p >
< p >< input type="file" name="upload_file"></ p >
< p >< input type="text" name="user"></ p >
< input type="button" value="提交" > </ form > < script >
$("#submitBtn").click(function(){
$("#ifr").load(iframeLoaded);
$("#form2").submit();
});
function iframeLoaded(){
alert(123)
} </ script >< em style=" font-family: ‘PingFang SC‘, ‘Helvetica Neue‘, sans-serif; "> </ em > |
views
1 2 3 4 5 6 7 8 | def index(request):
return render(request, "index.html" )
def upload_file(request):
print ( "FILES:" ,request.FILES)
print ( "POST:" ,request.POST)
return HttpResponse( "上传成功!" ) |