在當前項目中,我實現了一個UITableViewController
,它收集某些數據並通過用戶請求將其發送到後端服務器。 這一切工作正常和丹迪..但是,我有問題上傳圖像。使用ASIFormDataRequest將UIImage從UIImagePickerController發送到PHP服務器
只要我得到圖像om UIImagePickerController
併發送它,服務器似乎有問題。如果我將它編碼爲JPEG,PHP似乎無法理解它......如果我將它編碼爲PNG,整個請求似乎都被搞砸了。
但是,如果我選擇項目中存在的任何其他圖像(例如某個背景圖像),則上傳過程將完美運行!這是奇怪的部分。只要我使用UIImagePickerController中的圖像,它不會工作...儘管我確實得到了圖像(請參閱代碼中的addSubview,它的工作原理)。
這是我的代碼:
@interface ServiceViewController()
@property (nonatomic) UIImage *image;
[...]
@end
@implementation ServiceViewController
@synthesize image = _image;
[...]
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.image = [[info objectForKey:UIImagePickerControllerOriginalImage] copy];
[self.tableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}
- (void)sendToApi
{
// Send post data
NSString *urlString = [NSString stringWithFormat:@"%@/%@", API_BASE_DOMAIN2, @"requestservice"];
urlString = @"http://localhost/api/public_html/requestservice";
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setUseKeychainPersistence:YES];
[request setPostValue:[self.problems objectAtIndex:self.problem] forKey:@"problem"];
[request setPostValue:[self stationName] forKey:@"station"];
[request setPostValue:self.otherInformation forKey:@"information"];
[request setPostValue:[NSNumber numberWithInt:self.lock] forKey:@"lock"];
[request setUploadProgressDelegate:self];
[request setDidFinishSelector:@selector(didCompleteRequestService:)];
[request setDidFailSelector:@selector(processFailed:)];
[request setDelegate:self];
[request setRequestMethod:@"POST"];
// Upload an image
[request addData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)] withFileName:@"img.jpg" andContentType:@"image/jpeg" forKey:@"picture"];
UIImageView *view = [[UIImageView alloc] initWithImage:self.image];
[view sizeToFit];
[self.view addSubview:view];
[request startAsynchronous];
}
- (void)setProgress:(float)newProgress {
// [prgressView setProgress:newProgress];
NSLog(@"New progress: %f", newProgress);
}
- (void)didCompleteRequestService:(ASIHTTPRequest *)request
{
NSLog(@"Request completed");
NSLog(@"Result: %@", request.responseString);
}
服務器端:
var_dump($_FILES);
if ($this->_aPicture !== null) {
$sTarget = DIR_SERVICE . $this->_sFileNamePrefix . ".jpg";
var_dump($this->_aPicture);
var_dump($sTarget);
if (!move_uploaded_file($this->_aPicture['tmp_name'], $sTarget)) {
$this->returnHttpError(500);
}
}
$this->_writeDataToFile();
這是我的結果:
2013-03-21 00:04:04.859 AppName[15598:c07] New progress: 0.000000
2013-03-21 00:04:04.862 AppName[15598:c07] New progress: 1.000000
2013-03-21 00:04:04.863 AppName[15598:c07] Request completed
2013-03-21 00:04:04.863 AppName[15598:c07] Result: array(1) {
["picture"]=>
array(5) {
["name"]=>
string(7) "img.jpg"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
}
array(5) {
["name"]=>
string(7) "img.jpg"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
string(58) "/Users/paulp/Sites/api/service/K98XQPo7zx.jpg"
Sending error: 500
我已經嘗試了一堆不同的東西,如:
- How to upload image that was taken from UIImagePickerController
- Upload picture selected using UIImagePickerController 等等。
我還沒有變成例如MKNetworkKit,因爲沒有時間或預算(尚未),所以這不是一個解決方案。
我該如何解決這個問題?
正如我上面提到的那樣,由於某種原因,PNG表示將整個表格擰緊。使用它,PHP端看起來不會收到任何POST數據。 –
讓我問我們的服務器端程序員。 – iiFreeman
正如上面所寫,您會遇到與upload_max_filesize限制相關的問題。 錯誤= 1表示以下內容: 「上傳的文件超出了php.ini中的upload_max_filesize指令」 此設置的默認值爲2M。似乎你試圖上傳更多的2M。編輯php.ini並重新啓動php(或者apache,如果你使用它的話)。 – Karisters