hi there , so i am not sure if i am doing this the right way, but im successfully able to upload files(images) and store them in my defined folder and the link in my mysql database.
the error comes when i try to return the image with the HTTP RESPONSE were i get a image keyError.. i am not sure wht i am doing wrong..
here is my code:
def handle_uploaded_file(f,u):
profile=u.get_profile()
destination = open('images/users/'+
f.name, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
profile.save()
def upload_file(request,user_id):
u = get_object_or_404(User, pk=user_id)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'],u)
uploadedImage = form.cleaned_data['image']
return HttpResponse(uploadedImage.content,mimetype="image/jpeg")
else:
form = UploadFileForm()
c = {'form': form,'user' : u}
c.update(csrf(request))
return render_to_response('accounts/upload.html', c)
<form enctype="multipart/form-data" method="post" action="/accounts/{{
user.id }}/upload/">
{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" value="Submit" id="Save"/>
</form>
thanks in advance