I am trying to send image to my web site depending on user choice, every time the app should give the image's name which has been chosen by the user.
I am using Xcode 6 and the code below:
NSString *urlString = @"http://MyWebSite.com/sendImages.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[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"];
// file
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image,90);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:returnString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
And her is my PHP file:
<?
$myparam = $_FILES['userfile']; //getting image Here //getting textLabe Here
$target_path = "khadamati/";
if(move_uploaded_file($myparam['tmp_name'], $target_path . basename( $myparam['name'])))
{ echo "The file ". basename( $myparam['name']). " has been uploaded"; }
else
{ echo "There was an error uploading the file, please try again!"; }
?>
How can I change the myImage name in this line:
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
I tried to find any thin that would help me, but I could not, so please find the mistake in my booth code.
You should try this ,
uname,datestring ( i used here to define the user who has uploaded and the time when he uploaded ).
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@receipt%@.jpg\"\r\n", uname,datestring]] dataUsingEncoding:NSUTF8StringEncoding]];
i hope it will be helpful to you , uname and datestring both are strings !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.