上传文件只传输几个字节的数据-IOS

时间:2013-11-18 12:17:25

标签: php ios iphone objective-c file-upload

你好朋友, 我是iPhone新手。 我想在服务器上传视频。 我已经尝试了几种方法,但只有少量字节传输到服务器。 bellow是我的iOS代码

我在这里粘贴了我的代码。 在此先感谢。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *urlString = [info valueForKey:UIImagePickerControllerMediaURL];
    self.videoData = [NSData dataWithContentsOfURL:urlString];
  //  NSLog(@"%@",self.videoData);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

    videopath= [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/Video.mov",documentsDirectory]];

    BOOL success = [videoData writeToFile:videopath atomically:NO];

    NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
    NSLog(@"video path --> %@",videopath);


    [self uploadVideo];

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"Error");
}

-(void)uploadVideo
{
    self.responseData = [[NSMutableData alloc] init];
    NSString *urlStr;

    urlStr = [NSString stringWithFormat:@"http://iwillstudy.com/videomash/UploadToServer.php?pid=%@&uid=%@",[self.dictDetail objectForKey:@"id"],appDel.strEmailId];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"****";

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

    NSMutableData *body = [NSMutableData data];
    NSMutableDictionary *sendData = [[NSMutableDictionary alloc]init];

    [sendData setValue:videoData forKey:@"uploaded_file"];


    for (id key in sendData)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";filename=\" %@ \"\r\n\r\n", key,videopath] 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"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    [request setURL:[NSURL URLWithString:urlStr]];

    HUD = [[MBProgressHUD alloc] init];
    [self.view addSubview:HUD];
    [HUD setDelegate:self];
    [HUD setLabelText:@"Loading...."];
    [HUD show:YES];

    NSLog(@"FBLogin URL:%@",urlStr);

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

}

Bellow是我上传视频的php文件

<?php
    include "dbconn.php";
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) 
{
echo "success - file uploaded";
    $sql="INSERT INTO surveyvideos (name, path,added)
VALUES
('$file_path','$file_path',Now())";

if (!mysqli_query($con,$sql))
 {
 die('Error: ' . mysqli_error($con));
 }
echo "1";
} else{
echo "0";
}
?>

请帮助我们,自从过去3天以来我一直坚持这个,你能告诉我我一直在做什么错误。

0 个答案:

没有答案