Upload plist to remote server from iPhone - iphone

is it possible to upload an entire plist file to a remote server or to dropbox programmatically from an iPhone?
Any useful info will be helpful. If you have codes pls share thanks! :)
EDIT:
I'm currently using these codes i got it from Upload File to FTP Server on iPhone. But i'm having errors, my plist is in the document dir.
Function
- (BOOL) uploadData:(NSData *)plistData filename:(NSString *)filename {
NSString *urlString = #"http://site.net/upload.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:plistData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"Uploaded: %#", returnString);
return ([returnString isEqualToString:#"OK"]);
}
Call method
[self uploadData:[NSKeyedArchiver archivedDataWithRootObject:dbArray] filename:#"data.plist"];
PHP file
<?php
$uploaddir = '';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "OK";
} else {
echo "ERROR";
}
?>
It just keeps throwing "ERROR"...

For a generic server you just need to write script (e. g. using PHP) that will accept POST requests. You can then upload your file using NSURLConnection.
As usual, the official documentation has you covered. As for the server side script, that depends on what language/toolkit you choose. PHP’s move_uploaded_file() is just one of the possibilities.

Related

Image uploading from browser is fine but issue while uploading from iphone

I am uploading images to hosting server via browser (simple html form) the images can be uploaded without any problem but when I try to upload via iPhone it does not post the image to server.
Here is my code:
NSString *urlString = #"http://example.com/fileupload.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"a.jpg\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
NSLog(#"Response: %#",returnString);
I am using the same code on a different hosting server and working fine there.
I have checked the folder has 777 permissions.
Thanks.
Remove below lines, and it should work
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
First select an image
Code ::
#property(nonatomic, retain) UIImage *capturedImg;
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 1);
capturedImg = [UIImage imageWithData:dataImage];
}
For Uploading image,
Code ::
NSData *imgData = UIImageJPEGRepresentation(capturedImg, 0.8);
And, this imgData will be used for uploading image as data
Change ::
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]]; // 'userfile' to 'file'
Hopefully, it will help to you.
Thanks.
Thank you very much all for your help but the problem was on the server side. I don't know why the server sometimes rejects the file and sometimes it works fine.
Thanks again.

Error sending http post request with image

I'm stucked in this problem since days and i really need your help.
I'm actually trying to upload a file from my iphone app to a webserver trough a php script.
this is my method in Xcode:
-(void)sendPost{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:#"back.png"];
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
NSString *urlString = #"myscriptishere.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\";filename=\"myfile.png\"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSLog(#"%#",[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding]);
// Do any additional setup after loading the view.
}
and my php code is:
<?php
$domainpath = $_SERVER['DOCUMENT_ROOT'];
$target = "./upload";
$public = "/public/upload/";
$target = $target.basename( $_FILES['userfile']['name']) ;
$check = getimagesize($_FILES['userfile']['tmp_name']);
if (move_uploaded_file($_FILES["file"]["tmp_name"],
$domainpath. $public . $_FILES["file"]["name"]))
echo "YES";
else
echo "NO";
?>
I always receive (after few seconds, so I guess I'm uploading something) the server reply "NO".
I already checked I have permission on the folder I'm going to write in.
Any ideas on where the problem could be hidin?
Thanx in advance
Your problem is here:
NSString *urlString = #"myscriptishere.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];
You are posting to a filename. Your urlString variable should hold full address including protocol and domain (like #"http://localhost/myscriptishere.php").

How can i record voice on my app and then send it to server?

Im trying to build an application that will record voices (mic) on my iPhone,
and then will send them to store on my server.
can anyone help me with this one.
i even don't know where to began with this :)
Thank you very much,
Oded.
Just google it "how to record iphone" and you get it in a seconds.
After recording, file will create in your Documents folder.
Retrieve that path and extract data in NSData and send to server through POST method.
Sample code,
NSLog(#"SendingRecordedData");
NSString *urlString = [NSString stringWithFormat:#"http://your_server/fileupload.php"];
NSLog(#"Url:%#",urlString);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"pics\"; filename=\"record.caf\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data2]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
there is sample code with recording - SpeakHere on iOS developer site. After recording you can save it and send as any binary file.

How to send xml or image data from iphone to a rails 3 server?

I am developing a hybrid app (rails 3 + iPhone) and I want to send a number of large strings and images to a rails 3 server. I want to do a POST method from iPhone. Can someone help me on how to do this ? since in this case there will be no form in the views how should I accept the data ? Thanks in advance
Here is some code for uploading an image through POST to an URL:
UIImage *myImage = [UIImage imageNamed:#"example.png"];
NSData *imageData = UIImageJPEGRepresentation(myImage, 0.9);
NSString *url = [NSURL URLWithString:#"http://postingurl.com/posthere"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[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:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"fileid\"; filename=\"myfile.png\"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil e rror:nil];
[request release];
//Log return..
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
[returnString release];
You add more lines for Content-Disposition to include other fields.

Upload File to FTP Server on iPhone

I would like to implement logic that uploads file to FTP Server.
I am new in iPhone development, so want to figure out preferred approach. After browsing available API for this task I have managed to find only code that uses CFNetwork (that looks like not Objective C based).
There is also URL Loading System that uses NSURL etc which are Objective C based.
So, my question: Is it possible to use URL Loading System to implement file upload to FTP Server?
Thanks.
I use a PHP Page to post a file to and have PHP handle the uploading....
This code is used to upload a photo, but it can be adapted to work with any file.
PHP Code:
<?php
$uploaddir = 'photos/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "OK";
} else {
echo "ERROR";
}
?>
iPhone Code:
- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{
NSString *urlString = #"http://www.yourdomainName.com/yourPHPPage.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
return ([returnString isEqualToString:#"OK"]);
}
Method Call:
[self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:imageName];