后端代码

def upload(request):
if request.method == "GET":
return render(request,'upload.html')
if request.method =="POST":
pass def upload_file(request):
request.POST.get('username')
fafafa = request.FILES.get('fileobj')
img_path = os.path.join('static/img/',fafafa.name)
with open( img_path, 'wb') as f:
for item in fafafa.chunks():
f.write(item)
ret = {'code':True,'data':img_path}
return HttpResponse(json.dumps(ret))

views.py

html文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.c1 {
position: relative;
width: 100px;
height: 30px;
text-align: center;
line-height: 30px;
border-radius: 50%;
} .c2 {
position: absolute;
width: 100px;
height: 30px;
z-index: 10;
opacity: 0;
top: 0;
bottom: 0;
right: 0;
left: 0;
} .c3 {
display: inline-block;
background-color: blue;
color: white;
z-index: 9;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
}
</style>
</head>
<body>
<div class="c1">
<input class="c2" type="file" id="file11" name="file1"/>
<a class="c3">上传</a>
</div>
<h3>原生XMLHttpRequest提交</h3>
<input type="button" value="XML提交" onclick="xhrSunbmit();">
<h3>jquery提交</h3>
<input type="button" value="jquery提交" onclick="jqSunbmit();">
<hr/>
<hr/>
<h3>iframe提交</h3>
<form id="form1" action="/upload_file/" method="POST" enctype="multipart/form-data" target="ifm1">
{% csrf_token %}
<iframe id="ifm1" name="ifm1" style="display: none;"></iframe>
<input type="file" name="fileobj" onchange="changeUpalod();" />
{# <input type="submit" onclick="iframeSubmit();" value="Form提交"/>#}
</form>
<div id="preview"></div>
<script src="/static/jquery.js"></script>
<script src="/static/jquery.cookie.js"></script>
<script> function xhrSunbmit() {
var file_obj = document.getElementById('file11').files[0];
var fd = new FormData();
fd.append('username', 'root');
fd.append('fileobj', file_obj);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload_file/', true);
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var obj = JSON.parse(xhr.responseText);
console.log(obj);
}
};
xhr.send(fd);
} function jqSunbmit() {
var file_obj = document.getElementById('file11').files[0];
var fd = new FormData();
fd.append('username', 'root');
fd.append('fafafa', file_obj);
$.ajax({
url: '/upload_file/',
type: 'POST',
headers: {'X-CSRFtoken': $.cookie('csrftoken')},
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);
}
})
}
function changeUpalod(){
$('#ifm1').load(function(){
var text = $('#ifm1').contents().find('body').text();
var obj = JSON.parse(text); $('#preview').empty();
var imgTag = document.createElement('img');
imgTag.src = "/" + obj.data;
$('#preview').append(imgTag);
});
$('#form1').submit();
} /* function iframeSubmit(){
$('#ifm1').load(function(){
var text = $('#ifm1').contents().find('body').text();
var obj = JSON.parse(text); $('#preview').empty();
var imgTag = document.createElement('img');
imgTag.src = "/" + obj.data;
$('#preview').append(imgTag);
})
}*/
</script>
</body>
</html>

upload.html

最新文章

  1. 关于conky
  2. IOS开发之音频--录音
  3. Android数据存储-通过SharedPreferences实现记住密码的操作
  4. 粉笔网iPhone端使用的第三方开源库
  5. 【iCore3 双核心板_ uC/OS-III】例程六:信号量——共享资源
  6. des加密解密的两个方法
  7. 【转】Python 中map、reduce、filter函数
  8. hash函数为什么要选择对素数求余?
  9. iOS 热点、通话时候TabView的Frame调整
  10. hadoop2 环境的搭建(手动HA)
  11. objective-C 自定义对象归档的实现
  12. usaco5.5-Hidden Passwords
  13. c++多线程编程之互斥对象(锁)的使用之----死锁
  14. [转]Linux下CodeBlocks的交叉编译
  15. myeclipse maven tomcat插件 创建web工程
  16. PyCharm更换sublime类似主题
  17. HUE的自动化安装部署
  18. Unity3D之物理射线
  19. windows下mongodb基础玩法系列二CURD附加一
  20. 以太网,IP,TCP,UDP数据包分析【转】

热门文章

  1. 页面禁止刷新处理Js实现
  2. android和网络连接相关的类URL,URLConnection,HttpURLConnection,HttpClient
  3. 20175221曾祥杰 实验四《Android程序设计》
  4. git reset --hard 操作后的数据恢复
  5. NumericStream &amp;&amp; Stream综合练习
  6. Tensorflow 教程系列 | 莫烦Python
  7. Python实现比较两个列表(list)范围
  8. springboot上传文件过大,全局异常捕获,客户端没有返回值
  9. Java面试题全集(中)
  10. MathType的配置问题;将word中的公式转换为mathtype格式失败,缺少OMML2MML.XSL