2013-10-27 162 views
0

如何將iOS設備上的視頻上傳到Django服務器,以及如何將視頻保存到mp4格式的文件夾中。 我試過一些東西。這是我的iOS代碼:如何將視頻從iOS設備上傳到Django服務器

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() 

謝謝。

+0

感謝您發佈您的代碼,但請在您的問題中多加一點說明:您有什麼問題,您期望的結果是什麼,以及[您嘗試過什麼](http://whathaveyoutried.com)遠?通過[問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)將幫助我們更好地回答你的問題。謝謝! –

+0

你好,直到現在我已經讓選擇器控制器返回我的設備中的視頻的路徑,我試圖將其轉換爲數據並將其發送到我的python/django服務器,在這裏我希望它能夠接收它並保存到我的桌面/文件夾從我的設備視頻文件。問題是,它在django服務器,但我解決它 - > form = UploadFileForm(request.POST,request.FILES)。現在form.is_valid()是無效的,因爲它沒有罰款任何數據。 –

+0

我改變了我的iOS代碼 ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url]; [請求setPostValue:@「test」forKey:@「title」]; [request setFile:urlString forKey:@「file」]; [請求setRequestMethod:@「POST」]; [request setDelegate:self]; [request startSynchronous]; 現在它給我: - >檢測到不正確的NSStringEncoding值0x0000。假設NSASCIIStringEncoding。將在不久的將來停止這種兼容性映射行爲。 –

回答

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的,沒有找到該文件。

+0

是甚至格式正確的Python代碼? –

相關問題