如何将视频从iOS设备上传到Django服务器

时间:2013-10-27 21:53:26

标签: python ios django

如何将视频从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()

谢谢。

1 个答案:

答案 0 :(得分:0)

解决它。

以下是iOS代码:

- (void)imagePickerController:(UIImagePickerController *)picker    didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];

NSString *urlString=[urlvideo path];

NSString *str = serverAddress;
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setPostValue:[NSString stringWithFormat:@"%@.mp4",videoName.text] forKey:@"title"];
[request setFile:urlString forKey:@"file"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startSynchronous];

NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseStatusCode %@",[request responseString]);

[picker dismissViewControllerAnimated:YES completion:NULL];
}

Django代码:

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, request.FILES)

if form.is_valid():
    handle_uploaded_file(request.FILES['file'], request.POST.get('title'))
    return HttpResponse(json.dumps({"Status": 0}, sort_keys=True))
else:
    return HttpResponse(json.dumps({"Status": 1}, sort_keys=True))


def handle_uploaded_file(file, string, path=''):
    destination_file = open('%s/%s' % (settings.MEDIA_ROOT, str(path) + string), 'wb+')
    for chunk in file.chunks():
        destination_file.write(chunk)
    destination_file.close()

问题出在django,没找到文件。