I am getting a problem when i request to the server to upload and UImage
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/image.jpg"]; //UIImage *img = pickerImage;
[UIImageJPEGRepresentation(postingImage, 0.5) writeToFile:jpgPath atomically:YES];
NSData *imageData = UIImageJPEGRepresentation(postingImage, 0.5);
NSURL *url = [NSURL URLWithString:serverURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:imageData withFileName:#"image.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseStatusString %#",[request responseString]);
it is giving this responses
responseStatusCode 200
responseStatusString Error: No file uploaded
Do any one know about this, where i am doing mistake,
your help is much appreciated.
Thank you
Instead of
[request setData:imageData withFileName:#"image.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
Try as
[request addData:imageData withFileName:#"image.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
I did same with help of ASIHTTPRequest and get images successfully posted on server. This should work I guess as it is working in one of my live project.
Related
Here I am sending the file path or file with some parameters. But the server didn't get the video. So any problem in that code? Or anything you want to add something. Then please tell me. Any delegate method you want to tell me where I get error or setting in response .
NSString *path = [[NSBundle mainBundle] pathForResource:#"hangover" ofType:#"mp4"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:#"name"];
[request setPostValue:#"GUnit" forKey:#"title"];
[request setPostValue:#"133" forKey:#"user_id"];
[request setPostValue:#"8953d0e1c97ef83c9f0aff47" forKey:#"token"];
[request setPostValue:#"video song" forKey:#"desc"];
[request setPostValue:#"34" forKey:#"video_id"];
[request setPostValue:#"0" forKey:#"is_private"];
[request setFile:path forKey:#"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
Try this...
NSString *path = [[NSBundle mainBundle] pathForResource:#"hangover" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSData *movieData = [NSData dataWithContentsOfURL:fileURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:#"name"];
[request setPostValue:#"GUnit" forKey:#"title"];
[request setPostValue:#"133" forKey:#"user_id"];
[request setPostValue:#"8953d0e1c97ef83c9f0aff47" forKey:#"token"];
[request setPostValue:#"video song" forKey:#"desc"];
[request setPostValue:#"34" forKey:#"video_id"];
[request setPostValue:#"0" forKey:#"is_private"];
[self.request setData:movieData withFileName:#"hangover.mp4" andContentType:#"multipart/form-data" forKey:#"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
Hope it helps.
Add following code in your code and than you can write 2 delegate methods and can handle response.
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
- (void) uploadFailed:(ASIHTTPRequest *)request
{
//Upload succeeded;
}
- (void) uploadFinished:(ASIHTTPRequest *)request
{
//Upload failed;
NSError *error = [request error];
NSLog(#"Error Description-->%#",[error localizedDescription]);
}
To have indepth view for ASIHttpRequest please see documentation. Its very well documented.
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 upload large video file on server.I have a code that works fine for small videos but even 30 seconds video are also not uploaded.I have increased the timeOutSeconds also but that also of no use.
strurl contains the path of the video file.
Here's the code:-
NSString *str = [NSString stringWithFormat:#"%#/uploadVideo.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:self.gameID forKey:#"gameId"];
[request setFile:strurl forKey:#"uploadfile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:1000];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseString %#",[request responseString]);
Please help me
Thanks in advance!!