2016-04-23 48 views
1

视频正在上传,但显示0bytes时,我检查的网络数据显示为无网络数据。从ios上传视频到服务器显示0bytes

NSString *str1=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"demo.mp4"]; 
    NSLog(@"str1=%@",str1); 

    NSString *escapedUrlString = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSLog(@"escapedUrlString=%@",escapedUrlString); 

    NSURL *videoURL = [NSURL URLWithString:escapedUrlString]; 
    NSLog(@"videoURL=%@",videoURL); 

    NSData *newdata = [NSData dataWithContentsOfFile:escapedUrlString]; 
    webdata=[NSData dataWithData:newdata]; 
    NSLog(@"webData = %@",webdata); 

    NSData *videoData = webdata; 
    NSString *urlString = @"http://192.168.1.92/Abdul/IOS/Chat/upload/upload_vid.php"; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    NSString *boundary = @"---------------------------14737809831466499882746641449"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\".mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:videoData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPBody:body]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(@"returnString=== %@", returnString); 

} 


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

    webdata1 =[[NSMutableData alloc]init]; 

} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

    [webdata1 appendData:data]; 

} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

{ 
    NSLog(@"%@",error); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *responseString = [[NSString alloc]initWithData:webdata1 encoding:NSUTF8StringEncoding]; 

} 

<?php 

    "Upload: " . $_FILES["file"]["name"] . "<br />"; 
    "Type: " . $_FILES["file"]["type"] . "<br />"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
    "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 


     move_uploaded_file($_FILES["file"]["tmp_name"], 
     "upload/" . $_FILES["file"]["name"]); 
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 

?> 

回答

0

获取路径一样,

NSString *str1 = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"mp4"]; 

然后得到videoData一样,

NSData *videoData = [NSData dataWithContentsOfFile:str1]; 

然后通过这个videoData直接request body一样,

[body appendData:videoData]; 

删除额外的代码并将我用现有的这些代码替换掉。你的问题可能会解决。

更新:

NSString *videoURL = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"mp4"]; 
    NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath: videoURL]]; 

通过这个视频数据要求的身体和你得到的数据videoData首先检查。

和secoond的东西你应该使用AFNetworking发送数据。它使它更简单。

例如通过afnetworking

AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"you url"]]; 

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/videoupload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) 
          { 
           [formData appendPartWithFileData:videoData name:@"file" fileName:@"myVideo.mov" mimeType:@"video/quicktime"]; 
          }]; 


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request]; 

    [operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) 
{ 

NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 

}]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@"Video Uploaded Successfully");} 
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"Error : %@", operation.responseString);}]; 


[operation start]; 

希望这将有助于:)

+0

好,谢谢,我会检查 – VyTcdc

+0

不,我只为STR1为demo.mp4 – VyTcdc

+0

的NSString * STR1 = [[[一个NSBundle越来越nill mainBundle] resourcePath] stringByAppendingPathComponent:@“demo.mp4”];使用这一行,并根据你的旧代码将它传递给含内容的文件 – Lion

相关问题