0

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

谢谢你。

4

1 回答 1

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];
}

姜戈代码:

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,没有找到文件。

于 2013-10-28T09:10:27.140 回答