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 回答