Upload large file video to server in ios - 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.

Related

Image not upload to php server ios 10 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Image not upload to php server ios 10 It gives error as You did not select a file to upload.
This is the code
- (IBAction)changeProfilePic:(id)sender {
[[sharedClass sharedInstance]showprogressfor:#"Please wait"];
NSString *urlString = [ NSString stringWithFormat:#"http://domain.com/as/web_services/change_user_image?user_id=%#",self.useridString];
UIImage *image= profile_Image;
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
request.timeoutInterval = 10.0f;
request.HTTPMethod = #"POST";
NSString *boundary = #"011000010111000001101001";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary, nil];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data; name=\"projectId\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *stringData = [#"Content-Disposition: form-data;\
name=\"project\";\
fileName=\"photo.jpeg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:stringData];
[body appendData:[#"Content-Type: image/jpeg\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 error:nil];
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *statusDict = [[NSDictionary alloc]init];
statusDict = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSString *message = [statusDict valueForKey:#"msg"];
[SVProgressHUD dismiss];
NSLog(#" mesaage is %#", message);
}
Try this -
NSString *urlString = [ NSString stringWithFormat:#"http://domine.com/as/web_services/change_user_image?user_id=%#",self.useridString];
NSData *imageData = UIImageJPEGRepresentation(theImage, 0.5);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
request.timeoutInterval = 10.0f;
request.HTTPMethod = #"POST";
NSString *boundary = #"011000010111000001101001";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary, nil];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data; name=\"projectId\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *stringData = [#"Content-Disposition: form-data;\
name=\"project\";\
fileName=\"photo.jpeg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:stringData];
[body appendData:[#"Content-Type: image/jpeg\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 error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding
Remove space form the url
Copy below code to resolve your issue
NSString *urlString = [NSString stringWithFormat:#"http://domine.com/as/web_services/change_user_image?user_id=%#",self.useridString];

Upload image to server Ios.I have uiimageview and want to send imageview image to server for objective c

I am trying to take picture and send to data base (json using post method) in ios
-(IBAction)upload:(id)sender
{
// NSData *imageData = UIImagePickerControllerCameraCaptureModePhoto;
// UIImagePNGRepresentation(yourImage);
NSData *imageData = UIImageJPEGRepresentation(imageview.image,0.8);
NSString *urlString = [ NSString stringWithFormat:#"https://www.******/xyz/jsonurl.php?action=insert&lat=80.2626&long=29.64565&cat=1&note=&date=&sdkuserid=12345&nick_name=sandy&place_address=pluto&p_id=3&intid=%d,&name=userfile&filename=%#",1,imageData];
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:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%d\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"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(#" body %#",body);
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
Try this code and give me response
-(IBAction)upload:(id)sender
{
NSData *imageData = UIImageJPEGRepresentation(imageview.image,0.8);
NSString *urlString = [ NSString stringWithFormat:#"https://www.******/xyz/jsonurl.php?action=insert&lat=80.2626&long=29.64565&cat=1&note=&date=&sdkuserid=12345&nick_name=sandy&place_address=pluto&p_id=3&intid=1"];
urlString =[[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:Urlstr]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyyMMdd_hhmmss"];
NSString * fileName =[NSString stringWithFormat:#"%#",[formatter stringFromDate:[NSDate date]]];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"New%#.png\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"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 *ImageString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}
Here is the solution :
-(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId
{
NSURL *strURL = myUrl; //here is your url
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:strURL];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------147378098314876653456641449";
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]];
NSString *stringData = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Documents\"; filename=\%#.png\r\n",imageId];
[body appendData: [stringData dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"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);
}
Here 'Documents' is your folder name where you want to save this image to your backend server. Hope this helps you.... Enjoy!

how to upload multiple images to server ios?

I am trying to upload Image From my IOS device to server. when upload single image file to server it is successfully uploaded to my server. i want to upload multiple image files to server how can i do this.
// COnvert Image to NSData
NSData *dataImage = UIImageJPEGRepresentation([UIImage imageNamed:#"icon.png"], 1.0f);
// set your URL Where to Upload Image
NSString *urlString = #"http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/imgupload.php";
// set your Image Name
NSString *filename = #"icon";
// Create 'POST' MutableRequest with Data and Other Image Attachment.
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 *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.png\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:dataImage]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
// Get Response of Your Request
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"Response %#",responseString);
Muliple images upload attempted code
-(void)uploadImageToServer:(NSArray*)arrUploadData withTreatmentDetails:(NSDictionary *)dictArguments url:(NSString*)url
{
// COnvert Image to NSData
// set your URL Where to Upload Image
NSString *urlString = #"http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/imgupload.php";
// set your Image Name
// Create 'POST' MutableRequest with Data and Other Image Attachment.
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 *postbody = [NSMutableData data];
for (int i=0; i<[arrUploadData count]; i++)
{
NSData *dataImage = [[arrUploadData objectAtIndex:i] valueForKey:#"photographyData"];
NSString *filename = [[arrUploadData objectAtIndex:i] valueForKey:#"photographyimagename"];
if (dataImage)
{
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.png\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:dataImage]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"Response %#",responseString);
}
Hello, You can do it by using NSMutableArray
For Example :
NSMutableArray *arrayImageData = [[NSMutableArray alloc]init];
Take images from imagepicker
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *filePath,*path;
NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
[arrayImageData addObject:dataImage];
filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"screenShot1.png"];
[picker dismissViewControllerAnimated:YES completion:nil];
}
At the time of posting image on Server use following code
for (int i = 0; i < [arrayImageData count]; i++)
{
[request setPostValue:[[MYUtility getInstance] base64StringFromNSData:[arrayImageData objectAtIndex:i]] forKey:[NSString stringWithFormat:#"Image%d", i + 1]];
}
Hope this will help for you...I used this code for saving multiple images on server.

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