Upload zip file with Parameters [duplicate] - iphone

This question already has an answer here:
How can i upload file and other parameter using NSURLConnection
(1 answer)
Closed 9 years ago.
I am trying to upload a file using NSURLConnection using POST method. and also I need to send few parameters in Post like username etc.
I am trying this since morning Please help me guys.

NSString *urlString = #"http://test.com/upload.aspx";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"0xLhTaLbOkNdArZ"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
//Reading the file
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"test.zip"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
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=\"test.zip\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:myData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSError *returnError = nil;
NSHTTPURLResponse *returnResponse = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnResponse error:&returnError];
Try this

Related

objective-c - Doesn't upload file's content

I want to upload a local word (or rtf) file to my server. Uploads the file without content. There is no error code or anything..
NSString *urlString = #"http://myserver.com/upload-c.php";
NSString *filename=#"xx.docx";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"xx.docx"]];
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:data]];
[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(#"%#",returnString);
One obvious problem is with how you initialize your data variable.
Assuming "xx.docx" is the file you wish to upload, you have two problems.
You need to specify the full path to the file, not just the filename.
Since you are creating an NSURL from a file path, you need to use fileURLWithPath:, not URLWithString:.
Edit: Based on your comments, the file is located in the app's resource bundle. To get its fullpath you should do this:
NSString *fullPath = [[NSBundle mainBundle] pathForResource:#"xx" ofType:#"docx"];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
Also, this line:
[body appendData:[NSData dataWithData:data]];
should simply be:
[body appendData:data];
And get rid of the calls to stringWithString:. Example:
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
should be:
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
and:
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
should be:
NSString *boundary = #"---------------------------14737809831466499882746641449";

How to upload image to server from gallery as JSON on iOS

How can I upload an image to a server from the gallery on iPhone as JSON? I tried the following:
[Loading startLoading:YES];
NSString *urlString = [NSString stringWithFormat:#"http://railsboxtech.com/singing/jsn_song/register_response.php?accesstoken=%#",Access_Token];
//username, password, name, email, country
NSString *parameter = [NSString stringWithFormat:#"username=%#&password=%#&fname=%#&email=%#&lname=%#&btnImage4=%#",userField.text,passField.text,fnameField.text,emailField.text,lnameField.text,btnImage4];
NSConnection *conn = [[NSConnection alloc] initWithDelegate:self];
[conn sendRequest:urlString withParaMeter:parameter withMethod:#"POST" withTag:1];
[conn startAsynchronousRequest];
if you want to upload an image as a string you must convert your image data to Base64, then you can use your above method:-
For converting to a base64 string, refer to this Stack Overflow answer. You can then use your code for uploading.
Another Way
You can directly upload an image like so:
-(void)uploadImage
{
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSString *urlString = [ NSString stringWithFormat:#"http://yourUploadImageURl.php?intid=%#",1];
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", 1]] 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];
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

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.

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.