2011-09-22 94 views
1

有人請......#$%^請看看這個。使用setData和jpeg表示法遍歷調試器。使用ios4資源庫設置文件,嘗試新的PHP腳本,刪除asiHTTPrequest文件,並確保我有新的。還是一無所有......代碼的一半已經從網上或其他地方的示例中整合在一起。ios ASIFormDataRequest報告PHP上傳無效文件

這裏的目標是簡單地從相機膠捲中挑選一張照片,並上傳它,看起來很簡單,我有一個不同的PHP腳本,可以在桌面上正常工作,並從這裏抓取一個,因爲它更簡潔它也可以在桌面上運行。

所以倍率進行整理圖像拾取

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
UIImage *originalImage, *editedImage, *imageToSave; 

// dealing with a still image 
if(CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){ 

    editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage]; 

    originalImage = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage]; 

    /* 
    if(editedImage){ 
     imageToSave = editedImage; 
    } else { 
     imageToSave = originalImage; 
    } 
    */ 
    chosenImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 

    //_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(originalImage, 0.0)]; 
    //_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(chosenImage.image, 0.0)]; 

    UIImage *im = [info objectForKey:@"UIImagePickerControllerOriginalImage"] ; 
    UIGraphicsBeginImageContext(CGSizeMake(320,480)); 
    [im drawInRect:CGRectMake(0, 0,320,480)]; 
    _resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    _imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(_resizedImage, 0.0)]; 

    } 
    [picker release]; 
} 

然後上傳方法。

-(void)uploadPhoto 
{ 
//NSLog(@"image path inside uploadPhoto --> %@", _imagePath); 
NSLog(@"uploadPhoto"); 


//NSLog(@"%@", imageData); 

//_imageData = _imageData; 

NSString *unescapedURL = @"http://dev.xxxx.com/upload.php"; 

NSString * escapedURL = 
(NSString *)CFURLCreateStringByAddingPercentEscapes(
                NULL, 
                (CFStringRef)unescapedURL, 
                NULL, 
                (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
                kCFStringEncodingUTF8); 


NSURL *url = [NSURL URLWithString:unescapedURL]; 
//NSURL *url = [NSURL URLWithString:unescapedURL]; 

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setDelegate:self]; 
[request setRequestMethod:@"POST"]; 
//[request setStringEncoding:NSUTF8StringEncoding]; 
//[request addPostValue:@"submit" forKey:@"Submit"]; 
//[request setPostValue:@"Submit" forKey:@"Submit"]; 
[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"]; 
//[request setFile:_imagePath forKey:@"photo"]; 
//[request setFile:_imagePath withFileName:@"image5.png" andContentType:@"image/png" forKey:@"photo"]; 
[request setDidFailSelector:@selector(requestFailed:)]; 
[request setDidFinishSelector:@selector(requestFinished:)]; 
[request setTimeOutSeconds:500]; 
[request startAsynchronous]; 

NSError *error = nil; 
NSString *theString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error]; 
if(theString) 
{ 
    NSLog(@"Text=%@", theString); 
} 
else 
{ 
    NSLog(@"Error = %@", error); 
    NSString *localized = [error localizedDescription]; 
    NSString *localizedFail = [error localizedFailureReason] ? [error localizedFailureReason] : NSLocalizedString(@"not it", nil); 
    NSLog(@"localized error--> %@", localized); 
    NSLog(@"localizedFail--> %@", localizedFail); 

} 

[escapedURL release]; 

} 

話,完成/未選擇

-(void)requestFinished:(ASIFormDataRequest *)request 
{ 
    NSLog(@"requestFinished"); 
    NSString *respondingString = [request responseString]; 
    NSLog(@"response string--> %@", respondingString); 

    NSData *responseData = [request responseData]; 
    NSLog(@"%@", responseData); 
} 

-(void)requestFailed:(ASIFormDataRequest *)request 
{ 
    NSLog(@"requestFailed"); 
    //NSError *error = [request error]; 
    //NSLog(@"%@", [error description]); 
} 

幫助!溺水......

回答

0

這是一個PHP的問題。

move_uploaded_file($_FILES["file"]["tmp_name"] 

是問題。

,如果你看看這個

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"]; 

它改變了POST這樣的標準...

move_uploaded_file($_FILES["file"]["tmp_name"] 

需求是

move_uploaded_file($_FILES["photo"]["tmp_name"] 

加入

error_reporting(E_ALL); 
ini_set("display_errors", 1); 
print_r($_FILES); 

到PHP讓我看到了..

response string--> Array 
(
    [photo] => Array 
     (
      [name] => image4.jpg 
      [type] => image/jpeg 
      [tmp_name] => /tmp/phpCSXJgl 
      [error] => 0 
      [size] => 150854 
     ) 

) 
在選擇

由...定義

[request setDidFinishSelector:@selector(requestFinished:)]; 

我會在這裏做的是恢復了PHP到其原來的位置

move_uploaded_file($_FILES["file"]["tmp_name"] 

,改變setFile調用

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"file"]; 

一切都將與世界和睦,我要去吃點東西。乾杯!