我正在尝试将图像从iso设备上传到我的服务器,这是源代码,
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *smallImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString* serverurl = @"http://myserverurl/thumbnailCreator.php";
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
NSString *picPath = [path absoluteString];
[self uploadImage2:serverurl :smallImage :picPath];
[picker dismissModalViewControllerAnimated:YES];
}
-(bool)uploadImage2 :(NSString*)php :(UIImage*)image :(NSString*)picPath
{
int h = image.size.height;
int w = image.size.width;
//uploadname =filename;
NSString* filename = picPath;
NSError *error;
NSURLResponse *response;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:php]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[request addValue:filename forHTTPHeaderField:@"uploadfile"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadfile\";filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImagePNGRepresentation(image)]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close the form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
// Make synchronous request
NSData *data1 = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSString* outstr = @"NNOK";
NSString *returnString;
NSDictionary* json;
if ([data1 length] > 0 && error == nil)
{
returnString = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
//drill_debug_info("Response:%s",[returnString UTF8String]);
NSError* error1;
json = [NSJSONSerialization
JSONObjectWithData:data1 //1
options:kNilOptions
error:&error1];
outstr = [json objectForKey:@"Response"]; //2
}
if([outstr isEqualToString:@"OK" ])
{
return true;
}
else
{
return false;
}
}
但是我看不到上传到服务器上的文件,并且总是得到NNOK
结果。上面的代码可能有什么问题。
这是我的服务器php
<?php
$target_path1 = "images/";
$a=array('Response'=>OK);
$b=array('Response'=>NOK);
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
if(!empty($_FILES)){
//print_r($_FILES);
$target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']);
//echo $target_path1;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path1)) {
echo json_encode($a);
} else{
// echo "Return Code: " . $_FILES["uploaded_file"]["error"]."size:".$_FILES["uploaded_file"]["size"];
echo json_encode($b);
}
}
?>
答案 0 :(得分:1)
使用AFNetworking,尝试这样:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$strCmd = "/usr/sbin/CoinCreationBashFile ".$coinName." ".$coinNameAbreviation." ".$blockReward." ".$blockSpacing." ".$targetTimespan." ".$totalCoins." ".$firstAddyBit." ".$seedNode." ".$nameSeedNode." ".$headline." ".$blocksPerDay." ".$startingDifficulty." >> /tmp/BASH2log.txt 2>&1 &";
$return1 = $shell->exeCmd($strCmd);
//if there is any return from the script you can wait for the return
//or you can trigger like you have it now and get no return.
使用NSURLConnection:
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
NSString *stringUrl =@"http://www.myserverurl.com/file/uloaddetails.php?"
NSString *string =@"http://myimageurkstrn.com/img/myimage.png"
NSURL *filePath = [NSURL fileURLWithPath:string];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:userid,@"id",String_FullName,@"fname",String_Email,@"emailid",String_City,@"city",String_Country,@"country",String_City,@"state",String_TextView,@"bio", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:stringUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileURL:filePath name:@"userfile" error:nil];//here userfile is a paramiter for your image
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@",[responseObject valueForKey:@"Root"]);
Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[Alert_Success_fail show];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[Alert_Success_fail show];
}];