如何将视频从 iOS 设备上传到 Django 服务器,然后将其保存到 mp4 格式的文件夹中。我已经尝试了一些东西。这是我的 iOS 代码:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:serverAddress]];
NSData *postData = [[NSData alloc] initWithContentsOfFile:[self.movieURL absoluteString]];
[request setPostValue:@"test" forKey:@"name"];
[request addData:postData withFileName:@"testt.mov" andContentType:@"video/quicktime" forKey:@"file"];
[request setDelegate:self];
[request setCompletionBlock:^
{
NSLog(@"ResponseString:%@",[request responseString]);
}];
[request setFailedBlock:^
{
NSLog(@"Error");
}];
[request startSynchronous];
[picker dismissViewControllerAnimated:YES completion:nil];
这是我的python代码:
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
def upload_file(request):
if request.method != "POST":
return HttpResponseNotAllowed(permitted_methods=['POST'])
form = UploadFileForm(request.POST.get('name'), request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponse(json.dumps({"Status": 0}, sort_keys=True))
else:
return HttpResponse(json.dumps({"Status": 1}, sort_keys=True))
def handle_uploaded_file(file, path=''):
# filename = file._get_name()
destination_file = open('%s/%s' % (settings.MEDIA_ROOT, str(path) + 'test.mp4'), 'wb+')
for chunk in file.chunks():
destination_file.write(chunk)
destination_file.close()
谢谢你。