iPhone ASIHTTP error while uploading video file - iphone

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

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.

Objective-c: Downloading a PDF file

I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.mydomain.com/mypdffile.pdf"]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"<Application_Home>/Documents/"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.
Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

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

Uploading image or a video to server as a raw data without encoding

Is it possible to upload an media(image & video ) directly to server without encoding which I am picking using UIImagepickerController, if so please suggest me how to do it.Please provide a sample for this.
In case of video upload you can send a file without encoding on the server
Here is the code for that:
if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
}
//IN urlVideo You get the path of media file(video files)
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"];//value for gameID Parameter
[request setFile:strurl forKey:#"uploadfile"]; //setFile method is used for upload video files
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseStatusCode %#",[request responseString]);
I hope this might help you but please remember this does not work in case of image upload

Play Mp3 from after saving it locally on iPhone

I want to play an mp3 song which first will be downloaded locally and then it will be played.
I have written following code:
NSURL *url = [NSURL URLWithString:[songURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSURLResponse *response;
NSError *error;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *pathForSoundFile = [[paths objectAtIndex:0] stringByAppendingString:[NSString stringWithFormat:#"/my.mp3"]];
NSLog(#"%#",pathForSoundFile);
NSURL *myURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#",pathForSoundFile]];
[self playPreviewMethod];
-(void)playPreviewMethod
{
[songPreview play];
}
What I guess is that the file is not downloaded or converted in the mp3 format.
is your file getting downloaded?
Check out the folder Library-> applicationSupport-> iPhone Simulator -> your folder-> Documents
Chck out whether the file is downloaded. and if the file is getting downloaded then check out the name of the file whether it is my.mp3 or not. I think theres the problem you are not passing correct name while playing the file.
hAPPY cODING...