I am trying to upload image to django from iphone with ASIFormDataRequest.
in my django views.py
class UploadFileForm(forms.Form):
name = forms.CharField(max_length=255)
file = forms.FileField()
@csrf_exempt
def upload_image(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid(): ##this is always fail
newimage = Image(imgfile = request.FILES['image'])
newimage.save()
return HttpResponse('succeeded')
else:
return HttpResponse('failed')
in models.py
class Image(models.Model):
imgfile = models.FileField(upload_to='images/')
i got this in settings.py
MEDIA_ROOT = '/srv/www/media'
with the above code, i can't get the form validated, but if i remove form.is_valid(), then the image is successfully uploaded to /srv/www/media/images/
and the following is the iOS code:
formRequest = [ASIFormDataRequest requestWithURL:url];
[formRequest setPostFormat:ASIMultipartFormDataPostFormat];
formRequest.delegate = self;
[formRequest setData:UIImageJPEGRepresentation(image, 1.0f) withFileName:fileName andContentType:@"image/jpeg" forKey:@"image"];
[formRequest setPostValue:userName forKey:@"name"];
[formRequest setRequestMethod:@"POST"];
[formRequest setPostFormat:ASIMultipartFormDataPostFormat];
[formRequest startAsynchronous];
please, any suggestion is welcome and appreciated.
I don't know anything about iOS, but looks to me that you're providing a value for key name
, but you don't have that field in your form - you have jid
instead. Since that field is required, and you're not providing it, the form is not valid.