Upload File while application in background : ASIHTTPRequest with Phonegap - iphone

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

Related

Iphone image uploading -- 500 Error

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

iPhone ASIHTTP error while uploading video file

I am creating simple file uploaded.
Some unexpected behavior is happening with File upload.
When i am running build from my iMac to iPhone, it works fine without any issue.
But when i create an build(ipa) file. and install it from iTune. Request fails.
Might be there is issue with ASIHTTP?
You can take idea from following code.
-(IBAction)btnPostData_Clicked: (id)sender
{
NSString *file2 = [[NSBundle mainBundle] pathForResource:#"Ovation-Mike_Koenig-1061486511" ofType:#"mp3"];
NSData *file1Data = [[NSData alloc] initWithContentsOfFile:file2];
NSURL *audiourl = [NSURL URLWithString:#"http://alligator.netsmartz.us/iphone/iphone.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
NSData *postData = [[NSData alloc] initWithContentsOfFile:file2];
//SoundPath is your audio url path of NSDocumentDirectory.
[request addData:postData withFileName:#"Ovation-Mike_Koenig-1061486511.mp3" andContentType:#"audio/mp3" forKey:#"company_audio"];
[request setDelegate:self];
[request startSynchronous];
}

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.

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.

Objective-C and ASIHTTPRequest - response string problems

I'm using the ASIHTTPRequest package to send some data from the iPhone to my server and then when saved on server I recieve a response (a string) containing a url that I want to load in a webview. In theory this should be simple, but in reality I can't get this work at all. Seems to be some problems with encoding of the string I guess. If I NSLog out the response it seems to be totally fine, but it just refuses to load in the webview.
This is my request (been playing around with compression and encoding):
NSURL *url = [NSURL URLWithString:#"xxx"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setFile:imageData forKey:#"photo"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[request setAllowCompressedResponse:NO];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];//NSISOLatin1StringEncoding
[request start];
And this is my responder:
- (void)requestDone:(ASIHTTPRequest *)request{
NSString *response = [request.responseString];
NSLog(#"web content: %#", response);
[webView loadSite:response];
}
The NSLog seems fine, but site just won't load. Tried sending an NSString variable like NSString *temp = #"http://www.google.se"; to the loadSite function and that works fine, so the problem is not with the loading itself.
Would greatly appriciate any pointers or help in the obj-c jungle :)
/f
Try to do the following:
NSString *response = [[request responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
It will trim all whitespaces and newlines.
so turns out that a response is automatically ended with a \n (new linebreak). this didn't show up when logging the variable, neither when writing it to a textfield but when converting it to a urlobject and logging that i found it. debugging once again pays off ;)
woups, my responder is of course:
- (void)requestDone:(ASIHTTPRequest *)request{
NSString *response = [request responseString];
NSLog(#"web content: %#", response);
[webView loadSite:response];
}