upload image from iphone to the server folder - ios

I found few snippet from online to upload image from iphone to the server folder, it's showing to use server side scripting eg. use php at server side
<?php
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Uploaded!";
}else{
echo "Not Uploaded!";
}
/>
is it any possibilites to upload image directly to the server folder without the above code,
let say I want to upload into http://111.22.333.44/mysite/pics/ w/o any server side scripting, if can how this can be done

/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = #"http://iphone.zcentric.com/test-upload.php";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
/*
now lets create the body of the post
*/
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=\"ipodfile.jpg\"\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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
this code will be helpful for you....

If you setup your webserver to allow the PUT method you could do this. 99% of web servers only allow the GET and POST methods.
Having never done that, I can't give you much help. Here is a page that I got from google talking about it:
http://www.w3.org/QA/2008/10/understanding-http-put.html

Related

How to Send Byte array To Server [duplicate]

I want to send file to server path with a paramater "filepath" as one parameter, and file data as another paramater. How do I do it.Here in the following I am appending filepath with data. but I guess it is wrong please help
NSURL *nsurl =[NSURL URLWithString:_urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setURL:nsurl];
[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];
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:#"Model.png"], 0.0);
NSString *string = [NSString stringWithFormat:#"filepath=%#",_filePath];
NSData *pathData = [string dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:pathData];
//Image
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image\"; filename=\"%#\"\r\n",#"newFile.png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:nil];
NSLog(#"%#",dict);
thanks
You can't just do [body appendData:pathData]; at the start. You need to add it with the appropriate boundary and content information. So you should have a number of lines like:
Add the boundary
Add the content info
Add the content data
Add the boundary
And repeat from 2 for each additional piece of data to be added.
Check the spec for information on the appropriate content types and disposition info that needs to be added for each data type (perfect example for you is at the bottom).

Upload large file video to server in ios

I have to create application for upload video to server in ios. I am using code for upload video which is given below. This code upload small video and working nicely but above 5 mb video can't upload .
- (void)uploadvideo {
NSString *strurl=[NSString stringWithFormat:#"%#",appDele.DRUPAL_SERVICES_URL];
NSString *url=[[NSString alloc]initWithFormat:#"video_upload.php?user_id=%#&node_id=%#",appDele.UserId,appDele.strProductId];
NSString *urlString =[NSString stringWithFormat:#"%#%#",strurl,url] ;
NSLog(#"%#",urlString);
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:encodedString]];
[request setHTTPMethod:#"POST"];
//[request setTimeoutInterval:10000];
// NSInputStream *videoStream = [[[NSInputStream alloc] initWithData:data1] autorelease];
// [request setHTTPBodyStream:videoStream];
NSString *boundary = [NSString stringWithFormat:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"filename\"; filename=\"New.mp4\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog(#"%#New.jpg",appDelegate.MemberId);
[body appendData:[[NSString stringWithFormat:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:data1.length];
[body appendData:[NSData dataWithData:data1]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
}
Any suggestion on how to upload large video file in ios?
NSURLConnection is quite old, take a look at NSURLSession with it's delegates and blocks. Here's some tutorial, how to migrate from one to another.

Post request is not working but working fine in Browser?

This is my code which uses POST request to retrieve data but i am not able to get the desired results. There is no problem in url because it is showing JSON output on browser.
NSString *urlString = #"my url string";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"email_string\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n",allIOSContactsEmailAddresses] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data!=nil)
{
NSArray* array=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"array=%#",array);
}
}];
it is showing array=(null) in console..
Your multipart message body is not properly setup.
After the last part (you have only one) there needs to be a "close-boundary-delimiter". So, before you set the body for the request, you need to append the delimiter:
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Strictly, there's no need to close the "close-boundary-delimiter" with a CRLF ("\r\n").
There is no need to prepend the actual body value (allIOSContactsEmailAddresses) with a CRLF (you did not). This CRLF would count as body content. Actually, this can confuse the consumer.
Contrary, for text bodies, a closing CRLF may sometimes needed (depends on the server).
Note when adding a header, it must be closed with a CRLF (as you did). In order to close the header area a closing CRLF is required. Regarding this, your message is correct.
I just tried to use your code, and it succeeded without any problem.
Therefore your problem is in the server you send the request to, or in your NSURLConnection delegate methods which are not implemented properly.
You should also post the sending code (NSURLConnection usage).

Adding an "Upload" button to an apps that can capture photo using iPhone's Camera

I am a newbie of XCode and Objective-C, and I followed the example in http://www.techotopia.com/index.php/An_Example_iOS_4_iPhone_Camera_Application_(Xcode_4),
and so that I have an apps that can call the camera in device (in my case, I am using iPod Touch) and capture the picture, then it can shows the picture in the image view, or select the picture in the camera roll to show in the image view. This is tested and worked perfectly in my iPod Touch.
Now, I want to add an upload button that can upload the image that captured to my server
(e.g.: http://xxx.xxx.xxx.xxx/photo/) so that I can view the image in my computer, anyone can suggest a method to let me do so? Or if it can not be done, am I need to set-up a php based server to handle this upload problem.
You wanna upload an image to php server, in objective-c which you can do this :
//To convert your image file to raw.
NSData *imageData = UIImagePNGRepresentation(self.imageView.image);
NSString *urlString = #"http://sample.com/upload.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
//Boundary and Header.
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
//Your uploading file.
NSMutableData *bodyData = [NSMutableData data];
[bodyData appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
/*
* Declares the PHP received $_FILES variables.
* - $_FILES['_varName']['name'] = _sample.png
*/
[bodyData appendData:[#"Content-Disposition: form-data; name=\"_varName\"; filename=\"_sample.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[NSData dataWithData:imageData]];
[bodyData appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:bodyData];
//Received response of server.
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//Dumps it.
NSLog(#"%#", returnString);
May it can help you.

Uploading an audio file and receiving unexpected response in ios

Here it is my code to upload an audio file. I tried lot of questions and answers in stackoverflow. But still i am not getting any improvement on this. I have to upload an audio file with a php link. If i do that, i am getting response like "already exists" always. I tried to change the filename and upload it. still getting same response. Actually i have to receive a response from the server as link of a file i uploaded. I am not having any knowledge about php. Anyone help me with this. Any help would be appreciated to clear my issue.
NSData *fileData=[NSData dataWithContentsOfURL:soundFileURL];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://somesite.com/upload.php"]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSMutableData *body = [[NSMutableData alloc]init];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"fvgv4r346r4r4h3ur543ty5u54y5u4574545g4g5.samr\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[soundFileURL lastPathComponent]
[body appendData:[[NSString stringWithString:#"Content-Type: music/samr\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:fileData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
// NSLog(#"%#",[NSString stringWithUTF8String:[body bytes]]);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
[body appendData:[[NSString stringWithString:#"Content-Type: music/samr\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
I suspect this line
Also check your input variable

Resources