Iphone image uploading -- 500 Error - iphone

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];

Related

Upload a image on server iPhone

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.

Upload File while application in background : ASIHTTPRequest with Phonegap

I am using the ASIFormDataRequest for uploading file on server.
I am using ASIHTTPRequest to achieve the functionality of upload in background.
It working fine. But When application going to background then it fail to upload the file.
I try a lot of test with my code, even sometime it works for background too.
When I am uploading file and application going to background and then application again active then it upload successfully.
I notice that (but not sure) if application going in background for time more than the request time out time then it fail to upload. (I am not sure about it)
NSString *filePath = [arguments objectAtIndex:0];
NSString *fileName = [arguments objectAtIndex:1];
NSURL *url = [NSURL URLWithString:#"http://192.168.1.107/~amitb/test/upload.php"];
NSData *imageData = [[[NSData alloc] initWithContentsOfFile:filePath] autorelease];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.delegate = self;
[request setData:imageData withFileName:fileName andContentType:nil forKey:#"file"];
[request startAsynchronous];
Can anybody help me to upload file while application in background
Amit Battan
http://allseeing-i.com/ASIHTTPRequest/How-to-use#background_downloads_ios states that you can use:
[request setShouldContinueWhenAppEntersBackground:YES];

Post request from iPhone using ASIFormDataRequest not working

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.

Upload Large Video File on server

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!!

FTP upload iphone sdk

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