我编写了一个脚本,将文件POST到django视图以便使用ajax进行处理。但是,我似乎无法将文件作为文件传递。它只是在Django中输出为无。
Django View - 这个视图还没有完成,只是试图确保我有文件,它现在因为没有文件而在块上失败,因为你可以看到im打印用户名和文件,并且用户名打印但是文件是无。
def upload_profilepic(request):
print request.POST
id = request.POST['username']
f = request.FILES.get('file')
username = request.POST.get('username')
print f, username
path = 'C:\Users\Administraor\workspace\ChompProject\ChompMe\static\images\user_images%s' % id
destination = open(path, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
Ajax功能
$("#uppropic").click(function() {
var files = $('#profile_pic').val();
console.log(files);
var username = '{{user.username}}';
$.ajax({
url : "/profilepic/",
csrfmiddlewaretoken: '{{ csrf_token }}',
type: 'POST',
url : '/profilepic/',
enctype: "multipart/form-data",
data : {
csrfmiddlewaretoken: '{{ csrf_token }}',
file: $('#profile_pic').val(),
username: username,
},
success : function(data) {
document.getElementById('output').innerHTML = (data['message']);
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText);
document.getElementById('output').innerHTML = " Request Failed.";
}
});
return false;
});
表格
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
{% if is_user_profile %}
<div id='profile_pics'>
<form method='POST' enctype=multipart/form-data id='profile_pic_form'>
{% csrf_token %}
<input type='file' id='profile_pic'>
<button id='uppropic'> Upload Profile Pic</button>
{% for user in user_data %}
<input type='hidden' id='username' value='{{user.username}}'>
{%endfor%}
</form>
</div>
</div>
</div>
</div>
{%endif%}
所以我改为使用html iframe,
{% load staticfiles %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link href='{% static "css/bootstrap.css" %}' rel="stylesheet"></script>
<script type='text/javascript' src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type='text/javascript' src= '{% static "js/bootstrap.js" %}'></script>
</head>
<body>
<div>
<form method='POST' enctype='multipart/form-data'>
{% csrf_token %}
<div style="width:100%;background:#217fbc;color:#fff;padding:7px;">
<img src='{% static "images/icons/chomp.png" %}'><p style='float:right;margin-top:5px;'> Upload Pic</p>
</div>
<div style="width:100%;background:#fff;color:#352f29;padding:15px;">
<input type='file' name='file'>
</div>
<div style='width:100%;background:#4a525f;color:#FFF;padding:7px;height:50px;'>
<p style='float:right;'><input type="submit" class="btn btn-primary" value='Upload'></p>
</div>
</form>
</div>
</body>
这是我的views.py文件表单调用。我收到方法错误
[Errno 22]无效模式('wb')或文件名: 'C:\用户\ Administraor \工作空间\ ChompProject \ ChompMe \静态\图像\ user_images'
正如您所见,文件正在传递,var f正在设置为该文件。但是,我不确定问题是什么,因为这就是你如何在python中打开文件进行编写。 ˚F
@login_required
@csrf_protect
def upload_profilepic(request):
if request.method == 'POST':
f = request.FILES.get('file')
username = request.user
print f, username
path = 'C:\Users\Administraor\workspace\ChompProject\ChompMe\static\images\user_images%s' % id
destination = open(path, 'wb')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
context = {'user': request.user,
}
return render_to_response('profile_pic.html',context , RequestContext(request))