2013-06-21 149 views
0

在這裏搜索一個簡單的圖像上傳與ASIFormDataRequest後,我終於得到它的工作(有點),但意外的錯誤。ASIFormDataRequest上傳空白圖像

我的圖像上傳到我的服務器,但它是空白,0字節的大小。 這裏是我的代碼:

-(IBAction)uploadImage:(id)sender 
{ 
    NSData *data = UIImageJPEGRepresentation(self.imageView.image,0.6f); 
    NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload.png"]; 
    [data writeToFile:file atomically:YES]; 

    NSString *strURL = @"http://domain.com/upload.php"; 

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; 
    UIImage *image1=[UIImage imageNamed:file]; 
    NSData *imageData1=UIImageJPEGRepresentation(image1, 1.0); 
    [request setData:imageData1 withFileName:file andContentType:@"image/jpeg" forKey:@"avatar"]; 
    [request setRequestMethod:@"POST"]; 
    //[request appendPostData:body]; 
    [request setDelegate:self]; 
    [request setTimeOutSeconds:13.0]; 
    request.shouldAttemptPersistentConnection = NO; 
    [request setDidFinishSelector:@selector(uploadRequestFinished:)]; 
    [request setDidFailSelector:@selector(uploadRequestFailed:)]; 
    [request startAsynchronous]; 
} 
- (void)uploadRequestFinished:(ASIHTTPRequest *)request 
{ 
    NSLog(@" Error - Statistics file upload ok: \"%@\"",[request responseString]); 
} 

- (void)uploadRequestFailed:(ASIHTTPRequest *)request{ 
    NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]); 

} 

我的PHP代碼:

<?php 

$target = "./"; 
$target = $target . basename($_FILES['avatar']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['avatar']['tmp_name'], $target)) 
{ 
echo "The file ok"; 
} 
else { 
echo "Sorry, there was a problem uploading your file."; 
} 

?> 

起初我還以爲這是一個文件夾permision的事情,但我有一個簡單的HTML表單上傳測試它(與同php文件,未經編輯),文件全部上傳。

我知道這種上傳文件的方法是危險的,但目前它是我得到的唯一的東西。

任何想法爲什麼我的文件沒有上傳? 謝謝。

+0

你知道,ASIHTTPRequest不再被保留,並沒有因爲2011的更新? – Luke

+0

是的,我完全知道,但這並不能阻止(很多)使用它的人。 – Theodoros80

回答

0

好吧,我按照你的建議,實際上使用AFNetwork 這是我的代碼,以幫助別人。

NSData *imageToUpload = UIImageJPEGRepresentation(_imageView.image, 0.5f); 
    AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://domain.com"]]; 

    NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
     [formData appendPartWithFileData: imageToUpload name:@"avatar" fileName:fullFilename mimeType:@"image/jpeg"]; 
    }]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSString *response = [operation responseString]; 
     NSLog(@"response: [%@]",response); 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     if([operation.response statusCode] == 403){ 
      NSLog(@"Upload Failed"); 
      return; 
     } 
     NSLog(@"error: %@", [operation error]); 

    }]; 

    [operation start];