I need to upload a single file. I have tried ASIRequest or whatever it is called, but would like to have something with a detailed guide.
You'll want to use ASIFormDataRequest. You'll send your file as an NSData object in the request. Here's an example of sending a JPEG. If you have some other file type, you'll need to convert it to an NSData object for transmission to the server.
NSString *filename = #"image.jpg";
UIImage *image = [UIImage imageNamed:#"image.jpg"];
NSData *fileData = UIImageJPEGRepresentation(image, 0.9);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL urlWithString:#"http://www.yoursite.com"]];
[request setPostValue:filename forKey:#"name"];
[request setData:fileData forKey:#"file"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
// handle response from server
The ASI docs go over this in the section titled "Sending a form POST with ASIFormDataRequest" - http://allseeing-i.com/ASIHTTPRequest/How-to-use
Related
I am new to iphone development. I want to share image from my device to server which is handled by myself. I done this in both android as well as from iphone device. Its very well posted from Android, but i get 500 error message from server whenever i posted through iphone. I really dont know where i made mistake. Is there any protocol blocks iphone whenever i communicate with server.
NSString *uploadUrl = [NSString stringWithFormat:#"--My url-- "];
NSURL *url = [NSURL URLWithString:uploadUrl];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSMutableString *m_strUploadedFileName = [NSMutableString stringWithFormat:#"pic_%#_%#.png",dateString,[prefs objectForKey:#"EmailId"]];
NSData *imageData = UIImageJPEGRepresentation(m_imageFish.image, 0.2);
NSLog(#"image data is %#",imageData);
[request setData:imageData withFileName:m_strUploadedFileName andContentType:#"image/jpg" forKey:#"--server parameter to send-- "];
[request setDefaultResponseEncoding:NSISOLatin1StringEncoding];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request startAsynchronous];
I am new to iPhone. I want to upload an image to a server. I have url
http://lifestander.com:8080/fileupload/uploadfile.html?username=tom&password=1234&directory=ebay&filename=fantacy.jpg
I use the following method for uploading the file, but the result in console is: {"success":"false", "message":"Authentication Failed"}
I do not understand the output in the console. So please tell me what do I do wrong, or improve my code. What to do?
Thanks.
-(void)uploadFile{
UIImage* theImage = [UIImage imageNamed:#"images.jpg"];
NSString* path = [[NSBundle mainBundle] pathForResource:#"images" ofType:#"jpg"];
NSString *photoUploadURLString=#"http://lifestander.com:8080/fileupload/uploadfile.html";
NSURL *url = [NSURL URLWithString: photoUploadURLString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
// Upload an image
NSData *imageData = UIImageJPEGRepresentation(theImage,1.0);
[request addPostValue:#"tom" forKey:#"username"];
[request addPostValue:#"1234" forKey:#"password"];
[request addPostValue:#"ebay" forKey:#"directory"];
[request setData:imageData withFileName:#"images.jpg" andContentType:#"image/jpg" forKey:#"filename"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
}
I'm not sure whether it's a code fault, since there is no crash and you receive a response. {"success":"false", "message":"Authentication Failed"} means that there is something wrong with the authentication. Check if there is any login/password/acces key required to upload your file and if yes, implement it.
i want to upload an
UIImage
to a server.
the problem i dont know how to add it to my post to the server.
until now i sent post with thia:
NSString *reqURL = [NSString stringWithFormat:#"url"];
reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
there is any different way to do for image?
Use ASIHttpRequest
-(void)uploadImage{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Upload a file on disk
[request setFile:#"/Users/ben/Desktop/ben.jpg" withFileName:#"myphoto.jpg" andContentType:#"image/jpeg"
forKey:#"photo"];
// Upload an NSData instance
[request setData:UIImageJPEGRepresentation(myUIImage) withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
}
This answer points to this wrapper framework that should make it easy to do what you want without dealing with HTTP header strings and so on.
Basically, you'll turn your UIImage into an NSData object using a function like UIImagePNGRepresentation() and then upload it over HTTP.
Newbie to Rails/iOS here. I have a rails blog application. I'm trying to upload a post from iOS using an HTTP POST method with ASIFormDataRequest. Here's the code that get's called:
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setFile:#"star.png" forKey:#"image"];
[request startSynchronous];
NSString *response = [request responseString];
NSLog(#"response:: %#", response);
When I run this code, nothing happens. My server does not get contacted. I get response:: (null). Any idea why?
EDIT I found out that star.png needed to have its full file address. Now the POST works fine, but neither the name or image get saved into the db. A new post with empty name and image gets created. Any idea why?
why you are using localhost here?
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
use your web server ip instead of localhost
NSURL *url=[[NSURL alloc] initWithString:#"http://yourservername:3000/posts"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setPostValue:imageData forKey:#"image"];
[request startSynchronous];
For the "imagePath" is the path to your image. The full path.
I want to make an iPhone application to send an image to my server.
I want to draw something in iPhone (ex: a signature) as an image to POST binary image to my server (server is JSP). Please tell me what I have to do?
how to use iPhone UI?
how to make binary data from image, etc.
Firstly you can get an NSData object containing either a PNG or JPEG representation of the image data using the UIImagePNGRepresentation and UIImageJPEGRepresentation functions.
// To get the data from a PNG file
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
// To get the data from a JPEG file
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
(for more information see: UIImage Class Reference)
To finish to upload data from your iPhone to your server you can do this:
- (void)sendImage {
NSData *postData = [nsdata from your original image];
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:#"http://yoururl.domain"]]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
// Return data of the request
NSData *receivedData = [[NSMutableData data] retain];
}
[request release];
}
Use the drawrect method to do signatures on an UIImage. For that you have to use a UITouch delegate
and use the following to convert your UIImage object to NSData
// To get the data from a PNG file
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
// To get the data from a JPEG file
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);