0

ASIFormDataRequest を使用してファイルをサーバーにアップロードするコードを実装しました

私のコードは次のようになります:

   NSURL *url = [NSURL URLWithString:@"someurl"];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"medelix.db" ofType:nil];  
    NSData *myData = [NSData dataWithContentsOfFile:filePath]; 

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"file" forKey:@"serverIP"];


    [request setData:myData withFileName:@"Data.txt" andContentType:@"application/octet-stream" forKey:@"uploadedfile"];
   // [request setProgressDelegate:self];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(postFinished:)];
    [request setDidFailSelector:@selector(postFailed:)];

    [request startAsynchronous];

    NSData *newStringData = [request responseData];
    NSString *x = [[[NSString alloc] initWithData:newStringData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"result text from server is %@", x);

私のサーバー側のPHPは:

<?php

$target_path = '/Applications/XAMPP/xamppfiles/htdocs/myupload/';

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{ 
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else{ 
    echo "There was an error uploading the file, please try again!";
}

?>

実行中に見つけたバグ :request responseData は何も与えませんでした

ここで、ASIFormDataRequest を介してデータのブロックをサーバーに送信したいと考えています。ファイルの内容が完了するまで (チャンクごとにファイルを含むファイルを送信する)。

ASIFormDataRequestを使用してチャンクごとにデータを送信する方法をコードで提案できますか?

4

1 に答える 1

0

アップロードのネイティブ コードは次のようになります。

- (void)uploadSomethingFiveTimes:(NSURL *)url
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"medelix.db" ofType:nil];  

    NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath: filePath];
    ASINetworkQueue *myQueue=[[ASINetworkQueue alloc] init] ;
    [myQueue cancelAllOperations];
    [myQueue setUploadProgressDelegate:myProgressIndicator];

    [myQueue setDelegate:self];
    [myQueue setRequestDidFinishSelector:@selector(queueComplete:)];
    int index;
 NSData *myData=nil;
    int Filedata= [file seekToEndOfFile];
    Filedata=(Filedata/3);
   [file seekToFileOffset:0];

    NSLog (@"Offset = %llu", [file offsetInFile]);

    for (index=3; index>0; index--) {


         myData = [file readDataOfLength: Filedata];

        NSString *x1 = [[[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding] autorelease];
        NSLog(@"responseData=%@",x1);


         NSLog (@"Offset = %llu", [file offsetInFile]);

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(postFinished:)];
        [request setDidFailSelector:@selector(postFailed:)];
        [request timeOutSeconds];
        [request setNumberOfTimesToRetryOnTimeout:2];
        [request setPersistentConnectionTimeoutSeconds:120];
        [request setShowAccurateProgress:YES];
         NSString *ServerFileName=[NSString stringWithFormat:@"%@_%@",[self getcurrentdateTime],@"medelix.db"];

          [request setData:myData withFileName:ServerFileName andContentType:@"application/x-www-form-urlencoded" forKey:@"uploadedfile"];
        NSString *x = [[[NSString alloc] initWithData:[request responseData]encoding:NSUTF8StringEncoding] autorelease];
        NSLog(@"responseData=%@",x);
      ⠀
        [myQueue addOperation:request];
    }
    [myQueue go];

}




- (void)postFinished:(ASIFormDataRequest *)request
{
    // NSData *newStringData = [request responseData];
    NSString *x = [[[NSString alloc] initWithData:[request responseData]encoding:NSUTF8StringEncoding] autorelease];

    NSLog(@"Post Success %d [request responseData]=%@",[request inProgress],x);
    NSLog(@"Post Success %@" ,[request timeOutSeconds]);
}
- (void)postFailed:(ASIFormDataRequest *)request
{
    NSLog(@"Post Failed %d",[request inProgress]);
     NSLog(@"Post Failed %@",[request error]);
}




- (void)queueComplete:(ASINetworkQueue *)queue
{
    //[queue requestsCount];
  // NSLog(@"Max:%d", );
    NSLog(@"Max: %f, Value: %f", [myProgressIndicator progress],[myProgressIndicator progress]);
}

サーバー側のコードは次のようになります:--

+

<?php

    $target_path = '/Applications/XAMPP/xamppfiles/htdocs/myupload/';

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    $myFile=$_FILES['uploadedfile']['name'];
    echo $myFile." 1st line \n";

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
    { 
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
    } 
    else{ 
        echo "There was an error uploading the file, please try again!";
    }

    if(file_exists($myFile))

    {

        $file="server_".$_FILES['uploadedfile']['name'];


         //exec("chmod $target_path 0777");
         if( chmod($_FILES['uploadedfile']['tmp_name'], 777))
         {
         echo "YES!";
         }
        if( chmod($target_path, 777))
        {
            echo "YES";
        }
        else
        {
            echo "NO";
        }

       $fh = fopen($target_path,'r');
       // $file='/Applications/XAMPP/xamppfiles/htdocs/myupload/server';
       $ab=file_put_contents($file, fread($fh,filesize($target_path)),FILE_APPEND);
        echo "Fread".$ab;
    //    fwrite($fh,"\n");
        fclose($fh);
        echo $fh." 2st line \n".$target_path." \n";


    }

    unlink($target_path);


    ?>
于 2012-05-31T06:11:23.943 に答える