use this for sending image file. write down this code, once you have posted your text data on server.
UIImage *img=self.imgView.image;
NSData *imageData = UIImagePNGRepresentation(img);
NSString *urlString = [NSString stringWithFormat:@"%@upload.php",@"http://localhost:8888/ImageUploadiOS/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="uploadedfile"; filename="test%d.png"
",self.fileNum] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Image Return String: %@", returnString);
self.fileNum++;
UIAlertView *alertUpload=[[UIAlertView alloc] initWithTitle:@"DemoApp" message:@"Image Uploaded On Server" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertUpload show];
php code for stroing image data on server is below.
<?php
$uploaddir = 'uploads/';
$file = basename($_FILES['uploadedfile']['name']);
$uploadfile = $uploaddir . $file;
echo "file=".$file; //is empty, but shouldn't
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo $file;
}
else {
echo "error";
}
?>
don't forget to change url of request.
for sending multiple image code is
//image1
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="uploadedfile1"; filename="testing1.png"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//end here image1
//image2
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="uploadedfile2"; filename="testing2.png"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData2]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//end here image2
Php code for handling muliple images at once is
<?php
$uploaddir = 'uploads/';
$file1 = basename($_FILES['uploadedfile1']['name']);
$uploadfile = $uploaddir . $file1;
echo "file=".$file1; //is empty, but shouldn't
if (move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $uploadfile))
{
//echo $file;
//echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
echo 'file1 moved';
}
else
{
echo "error";
}
$file2 = basename($_FILES['uploadedfile2']['name']);
$uploadfile = $uploaddir . $file2;
echo "file=".$file2; //is empty, but shouldn't
if (move_uploaded_file($_FILES['uploadedfile2']['tmp_name'], $uploadfile))
{
//echo $file;
//echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
echo 'file2 moved';
}
else
{
echo "error";
}
?>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…