2012-01-17 151 views
0

我使用ASIHTTPRequest使用PHP上傳照片。 PHP方面已經證明可以工作(我正在使用它的android),我試圖讓iOS組件工作。使用ASIHTTPRequest上傳照片

這是我上傳:

NSString *myurl = @"http://mydomain.tld/php/upload.php?casenum="; 
    myurl = [myurl stringByAppendingFormat: casenumber]; 
    NSString *fixedURL = [myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *url = [NSString stringWithFormat:@"%@",fixedURL]; 

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    NSData *imageData = UIImageJPEGRepresentation(image1.image, 90); 
    [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"file"]; 

    [request setCompletionBlock:^{ 
     NSString *responseString = [request responseString]; 
     NSLog(@"Response: %@", responseString); 
    }]; 
    [request setFailedBlock:^{ 
     NSError *error = [request error]; 
     NSLog(@"Error: %@", error.localizedDescription); 
    }]; 

    [request startAsynchronous]; 

伊夫發現這個在互聯網上,當我運行它,它失敗,出現錯誤2012-01-17 10:52:16.939 MyApp[30373:707] Error: NSInvalidArgumentException

從很多,我在網上找到的文檔和示例,這應該起作用,但它不是,正如你可以看到的例外。任何幫助消除扭結?如果您需要其他信息,我很樂意發佈。

+0

設置調試器停止異常,它停止的位置應該讓你知道哪個參數是無效的。 – JosephH 2012-01-17 20:17:26

回答

0

在製作NSURL的過程中會出現一些錯誤。首先,它是隻需要轉義的參數,而不是整個URL,這樣

NSString *myurl = @"http://mydomain.tld/php/upload.php?casenum="; 
myurl = [myurl stringByAppendingFormat: casenumber]; 
NSString *fixedURL = [myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

應該

NSString *myurl = @"http://mydomain.tld/php/upload.php?casenum="; 
NSString *escapedCasenumber = [casenumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
myurl = [myurl stringByAppendingFormat:escapedCasenumber]; 

其次,你再嘗試一個NSString分配給NSURL,所以

NSURL *url = [NSString stringWithFormat:@"%@",fixedURL]; 

應該

NSURL *url = [NSURL urlWithString:myurl]; 

最後,第二個參數傳遞給UIImageJPEGRepresentation應該是在0.0和1.0之間的浮動值,所以我猜

NSData *imageData = UIImageJPEGRepresentation(image1.image, 90); 

應該

NSData *imageData = UIImageJPEGRepresentation(image1.image, 0.9); 

如果仍然無法正常後,這些工作然後按照JosephH的建議,使用調試器來確定哪一行導致異常。