How to upload String and Image using ASIFormDataRequest? - ios

I want to upload follow Log Strings along with UIImage,
HOw can I achieve such objective????
I can see to send only image , but i want to send image along with text data using this ASIFormDataRequest, using POST request type.
NSLog(#"data is here %#\n%#\n%#\n%#\n%#\n",deviceID, postID, name, message, picture);
UIImage *image= [UIImage imageNamed:#"profile_avatar.png"];
NSString *strURL = #"https://abc.abc.com/comments/create";
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; // Upload a file on disk
// NSString *filename1=[NSString stringWithFormat:#"friendship.jpg"];
UIImage *image1=image;
NSData *imageData1=UIImageJPEGRepresentation(image1, 1.0);
[request setData:imageData1 withFileName:#"filename" andContentType:#"image/png" forKey:#"avatar"];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Authorization" value:[NSString stringWithFormat:#"Basic %#",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:#"username123:pass123"] dataUsingEncoding:NSUTF8StringEncoding]]]];
[request setValidatesSecureCertificate:NO];
//[request appendPostData:body];
[request setDelegate:self];
[request setTimeOutSeconds:3.0];
request.shouldAttemptPersistentConnection = NO;
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
Thanks

There is one method available to send parameter as well
[request setPostValue:#"YourValue" forKey:#"YourKey"];

Related

How to use GData framework in iOS app to upload images to Picasa?

In my app, I have an imageView, I want to post the image to Picasa.
I got some suggestion to use GData Framwork. I downloaded it from
code.google.com/p/gdata-objectivec-client/downloads/list
But, when I add the .a file in my Build phases, it is displayed as red. I have tried all the method in SO, as well as other forums.
Is there any other way to upload image to Picasa.
Any help would be appreciated.
I don't have an app to test this with, but I've translated the php-curl solution from this SO question.
NSString *authCode = [NSString stringWithFormat:#"GoogleLogin auth=",#"ACCESS CODE FROM OAUTH2"];
NSString *userId = #"USER ID GOES HERE";
NSString *albumId = #"ALBUM ID GOES HERE";
NSString *albumUrl = [NSString stringWithFormat:#"https://picasaweb.google.com/data/feed/api/user/%#/albumid/%#",userId, albumId];
// To get the data from a PNG file
NSData *postData = UIImagePNGRepresentation([UIImage imageNamed:#"YOUR IMAGE"]);
// To get the data from a JPEG file
//NSData *postData = UIImageJPEGRepresentation([UIImage imageNamed:#"YOUR IMAGE"], 0.9f);
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
// Init and set fields of the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:#"POST"];
[request setURL:[NSURL URLWithString:[NSString stringWithString:albumUrl]]];
[request setValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"2" forHTTPHeaderField:#"GData-Version"];
[request setValue:authCode forHTTPHeaderField:#"Authorization"];
[request setHTTPBody:postData];
NSURLResponse* rep = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&rep error:&err];
if (data && !err) {
//Do something with the response
}
Hope this helps.

Upload Image with dictionary

I want to upload image to server from iphone. Using ASIHttpRequest and ASIFormDataRequest i have sucessfully uplaoded picture to server by
[request setdata:...]
But I also want to send userId to server with image. How to achieve this? Here is my code...
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:URLSaveImage]];
__unsafe_unretained ASIFormDataRequest *request = _request;
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];
[request setUploadProgressDelegate:self];
NSData *data=UIImageJPEGRepresentation(proImage.image,1.0);
[request setData:data
withFileName:#"profile.jpg"
andContentType:#"image/jpeg"
forKey:#"file"];
For POST method, use setPostValue: forKey: with the setdata:. You dont need to do any additional work . Just post the variable along with image.
[request setPostValue:VALUE forKey:KEY];
[request setData:data withFileName:#"profile.jpg" andContentType:#"image/jpeg"
forKey:#"file"];

How to convert UIImage or NSData to byte Array and post to server?

How to convert UIImage or NSDate to byte array and post to server.
In my app i have to convert UIImage to byte array and i have to post.
for post i am using as ASIFormDataRequest.
My code is:
NSUInteger len = [self.dataImage length];
Byte *byteData= (Byte*)malloc(len); //converting date to byte array
[self.dataImage getBytes:byteData length:len];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setRequestMethod:#"POST"];
[request addData:self.dataImage withFileName:#"Image.png" andContentType:#"image/png" forKey:#"photo"];
[request setDelegate:self];
[request startAsynchronous];
Try Like this,
NSData *imageData = UIImagePNGRepresentation(self.dataImage ,0.1);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setRequestMethod:#"POST"];
[request addData:imageData withFileName:#"Image.png" andContentType:#"image/png" forKey:#"photo"];
[request setDelegate:self];
[request startAsynchronous];
I solved this by replacing
[request addData:imageData withFileName:#"Image.png" andContentType:#"image/png" forKey:#"photo"];
with
[request appendPostData:self.dataImage];

How to convert image into binary format in iOS?

I am working on a project where I need to upload image to my server. I want to store my image's binary data to BLOB data type field in database. Therefore I need to convert my image into binary format. So that it can be saved on servers database.
So How to convert an image into binary format? Please advise.
You can use the CoreGraphics' method UIImagePNGRepresentation(UIImage *image), which returns NSData and save it.
and if you want to convert it into again UIImage create it using [UIimage imageWithData:(NSData *data)] method.
Demo to send your UIImage to Server
- (void)sendImageToServer {
UIImage *yourImage= [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSString *postLength = [NSString stringWithFormat:#"%d", [imageData length]];
// Init the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:#"POST"];
[request setURL:[NSURL URLWithString:[NSString stringWithString:#"http://yoururl.domain"]]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:imageData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
}
[request release];
}
Use:
NSData *data = UIImageJPEGRepresentation(image, 1.0);
//OR
NSData *data = UIImagePNGRepresentation(image);
According to requirement.

NSMutableURLRequest transform to ASIFormDataRequest

I have writen the fellowing code:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
postStr = #"user_name=Thomas Tan&phone=01234567891&password=123456";
NSData *myRequestData = [NSData dataWithBytes:[postStr UTF8String] length:[postStr length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: myRequestData];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"%#",responseString);
it works well,but now I want to use asihttprequest framework,so how to change the above code,I have writen the code,but it can't get the correct result and just get the server error infomation.so what's the problem?
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *requeset = [ASIFormDataRequest requestWithURL:url];
[requeset setRequestMethod:#"POST"];
[requeset setPostValue:#"Thomas Tan" forKey:#"user_name"];
[requeset setPostValue:#"01234567891" forKey:#"phone"];
[requeset setPostValue:#"123456" forKey:#"password"];
[requeset startSynchronous];
NSError *error = [requeset error];
if (!error) {
NSString *re = [requeset responseString];
NSLog(#"%#",re);
}
NSLog(#"%#",error);
thank you in advance.
UPDATE:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request appendPostData:[#"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *re = [request responseString];
NSLog(#"%#",re);
}
NSLog(#"%#",error);
I use the above code ,It also can't get the same result,and error is not nil.
Your ASIHTTP code is not doing the same thing as your NSURLConnection code.
ASIFormDataRequest will automatically:
set the Content-Type header to application/x-www-form-urlencoded
URL-encoded your parameters
That's usually exactly what you want, but if you're getting the correct behavior with your NSURLConnection code and incorrect with ASIHTTP, then you need to change to a custom ASIHTTP POST and use ASIHTTPRequest, not ASIHTTPFormDataRequest, and then manually set the Conten-type back to application/x-www-form-urlencoded:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded"];
[request appendPostData:[#"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
Doing this, and inspecting exactly what was sent to the server using Wireshark, I can see that the POST data sent is still not quite identical (ASIHTTP on the left, NSURLConnection on the right):
But the content type, length, and actual data is identical.
At this point, I'd expect your server to return the same result.
If it still doesn't, you can edit the ASIhTTP request parameters to match.

Resources