Failed to move file error in ASIHTTPRequest - iphone

I am using ASIHTTPRequest for downloading file from server but its giving error
Failed to move file from '/Users/admin/Library/Application
Support/iPhone
Simulator/3.1.3/Applications/8650FFE4-9C18-425C-9CEE-7392FD788E6D/Documents/temp/test.zip.download'
to '/Users/admin/Library/Application Support/iPhone
Simulator/3.1.3/Applications/8650FFE4-9C18-425C-9CEE-7392FD788E6D/Documents/test.zip'
can any body tell mw this error what wrong in my code......
NSURL *url = [NSURL URLWithString:#"http://wordpress.org/latest.zip"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:#"%#/test.zip", [dirArray objectAtIndex:0]];
//NSString *tempPath = [NSString stringWithFormat:#"%#test.zip", NSTemporaryDirectory()] ;
NSString *tempPath =[NSString stringWithFormat:#"%#/temp/test.zip.download", [dirArray objectAtIndex:0]];
// The full file will be moved here if and when the request completes successfully
[request setDownloadDestinationPath:path];
[request setTemporaryFileDownloadPath:tempPath];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[[self queue] addOperation:request]; //queue is an NSOperationQueue

do you already have a temp.zip in that location ?

It also happens if you didn't set the destination path correctly, using this method setDownloadDestinationPath: of ASIHTTPRequest...

Your call
[request setTemporaryFileDownloadPath:tempPath];
is not necessary, and is more than likely the source of your error.

Related

Uploading a file with ASIFormDataRequest does not work

I am using the ASIHTTPRequest framework trying to upload a file to my websever via iPhone.
Below is the code. As long as I don't use the setFile method, I get a 200 back from the server, so everthing is fine. As soon as I implement setFile, the server returns 0. I would expect a 401 or anything like this, as I could imagine that I deal with an authentication issue.
My server is an IIS, why I use the NTLM way in the request. Do I miss something?
NSInteger httpStatus;
NSString *httpResponseString;
NSError *httpRequestError;
NSArray *paths = [[[NSArray alloc] init] autorelease];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [documentsDirectory stringByAppendingPathComponent:#"abiliator_basis_de_ar.xml"];
NSURL *myURL = [NSURL URLWithString: #"http://my.url.com/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
[request setShouldPresentCredentialsBeforeChallenge:NO];
[request setUsername:#"myUserName"];
[request setPassword:#"myPassword"];
[request setDomain:#"myDomainName"];
[request setFile:[NSURL URLWithString:filename] forKey:#"xml"];
[request startSynchronous];
httpRequestError = [request error];
httpResponseString = [request responseString];
httpStatus = [request responseStatusCode];
if (!httpRequestError) {
httpStatus = [request responseStatusCode];
NSLog(#"Class %#, Method: %# - OK login and filetransfer successful '%i'", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
}
else {
NSLog(#"Class %#, Method: %# - Error '%i' occurred sending the http request", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
}
Yes the file exists, here the result from the ls:
rene-stegs-macbook-pro:~ renesteg$ ls -ltr '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml'
-rw-r--r--# 1 renesteg staff 1062 22 Mai 13:44 /Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml
And here the value of filename:
Filename string is: '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml
You are doing something wrong while uploading file.
it need to like this
NSFileManager *fileManager = [NSFileManager defaultManager];
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setRequestMethod:#"POST"];
[request setTimeOutSeconds:120];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request addRequestHeader:#"Content-Type" value:#"multipart/form-data"];
if ([fileManager fileExistsAtPath:filePath] == YES) {
[request setFile:filePath withFileName:#"test.xml" andContentType:#"xml" forKey:#"FieldName"];
}
Here file path need to set for fromdata request.
Hope this will work for you.
You should probably try usingsetData:forKey: and send the data of the xml file, example below..
.....
NSURL *myURL = [NSURL URLWithString: #"http://my.url.com/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
[request setShouldPresentCredentialsBeforeChallenge:NO];
[request setUsername:#"myUserName"];
[request setPassword:#"myPassword"];
[request setDomain:#"myDomainName"];
[request setData:[NSData dataWithContentsOfFile:fileName] forKey:#"xml"];
[request startSynchronous];
.....
Also, you should probably make this an asynchronous request.
i think the error occurs because you should use:
[request setFile:[NSURL fileURLWithPath:filename] forKey:#"xml"];
Ok, the issue was between the line of Neels comment: missing PHP.
And this of course needs to be called in the URL, so the correct way to build the URL is:
NSURL *myURL = [NSURL URLWithString: #"http://my.url.com/upload.php"];
This way I got the mapping my iPhone httpRequest code and the PHP.
Here's the required PHP code:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['xml']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['xml']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['xml']['name']). " has been uploaded";
}
else {
echo "Error uploading". basename( $_FILES['xml']['name']). " occured";
}
?>

Is this the correct way of downloading a file with ASIHTTPRequest?

Is this the correct way of downloading a file with ASIHTTPRequest? As you can see, I'm trying to download the file to the documents directory and am naming Test.mov. The only part is... the file doesn't download. I'm still confused by how ASIHTTPRequest works, so all help is appreciated!
-(IBAction)download {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:#"https://www.chillysky.com/Test.mov"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:#"%#/Test.mov", documentsDirectory];
[request setDownloadDestinationPath:file];
[request setDownloadProgressDelegate:myProgressIndicator];
[request setDelegate:self];
NSLog(#"Downloading File");
}
Got it! Forgot this code:
[request startAsynchronous];

ASIHTTPRequest - download problem

I try to download a file from my server, this is the code, on my console I see the xml file, but I can't save it.
Where is the problem for you?
- (IBAction)grabURL:(id)sender{
NSURL *url = [NSURL URLWithString:#"http://www.endurodoc.net/photo/data.xml"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"%#",response);
}
else{
NSLog(#"Errore");
}
//[request setDownloadDestinationPath:#"/Users/kikko/Desktop/data.xml"];
// SAVED PDF PATH
// Get the Document directory
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved pdf location
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:#"data.xml"];
// TEMPORARY PDF PATH
// Get the Caches directory
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your temp pdf location
NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:#"data.xml"];
// Tell ASIHTTPRequest where to save things:
[request setTemporaryFileDownloadPath:tempPdfLocation];
[request setDownloadDestinationPath:pdfLocation];
}
You need to put:
[request setTemporaryFileDownloadPath:tempPdfLocation];
[request setDownloadDestinationPath:pdfLocation];
before:
[request startSynchronous];
ASIHTTPRequest does the file saving when the request is made, so if you set those properties after the request has already happened then nothing will happen.

How to obtain the zip filename being downloaded in iphone

Currently using SSZipArchive method to download the zip file and unzip at Documents directory folder. I am downloading the zip file name from URL and currently unaware of the file name because it changes each time there are any updates. How can I retrieve the file name when I receive my data ?
- (void)viewDidLoad {
fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
directoryPath = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/ZipFiles.zip", directoryPath];
NSURL *url=[NSURL URLWithString:#"http://www.abc.com/test/test.cfc?id=123"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[fileManager createFileAtPath:filePath contents:urlData attributes:nil];
[SSZipArchive unzipFileAtPath:filePath toDestination:directoryPath];
NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
///***Answer to my own question for future needs****//
NSString *fileName = [response suggestedFilename];
}
NSURLResponse has a method - (NSString *)suggestedFilename which will attempt to get the file name in this order.
A filename specified using the content disposition header.
The last path component of the URL.
The host of the URL.
The content disposition header would be the best solution so make sure that the server sets it.

upload plist file on server in iphone

i have created a plist file other than the default one that exists.
Can i upload this plist file onto the server
I tried ASIFormDataRequest.
I was able to upload the image and text file but when i try it with plist it throws error at point shown in bold:
Code:
networkQueue = [[ASINetworkQueue queue] retain];
NSString *filePath = [[[NSBundle mainBundle]
resourcePath] stringByAppendingPathComponent:
[#"test" stringByAppendingString:#".plist"]];
ASIFormDataRequest *request =[ASIFormDataRequest
requestWithURL:[NSURL URLWithString:#"http://allseeing-i.com/ignore"]];
[request setPostValue:#"my_test" forKey:#"share_test"];
[request setFile:filePath
withFileName:[test stringByAppendingString:
#".plist"] andContentType:#"propertylist/plist" forKey:#"mytest"];
[request setDelegate:self];
[request setDidFailSelector:#selector(requestFailed:)];
[request setDidFinishSelector:#selector(gotTheResponse:)];
[networkQueue addOperation: request];
[networkQueue go];
is it really possible?
or should i go ahead with xml
though plist is also an xml format
but still i want to know and what should i do?
networkQueue = [[ASINetworkQueue queue] retain];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
// hyphen(-) joins file name with the timestamp for uniqueness
NSString *theme_name1 = [[[theme_name stringByAppendingString:#"-"]
stringByAppendingString:dateString]
stringByReplacingOccurrencesOfString:#" " withString:#"_" ];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *path = [documentsPath stringByAppendingPathComponent:
[file_name stringByAppendingString:#".plist"]];
id plist = [[NSDictionary alloc] initWithContentsOfFile:path];
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
NSString *xml_string = [[NSString alloc] initWithData:xmlData
encoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:#"myurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"file_name1 forKey:#"filename"];
[request setPostValue:#"user" forKey:#"sharedby"];
[request setPostValue:xml_string forKey:#"data"];
[request setUsername:#"hello"];
[request setPassword:#"world"];
[request setDelegate:self];
[request setDidFailSelector:#selector(requestFailed:)];
[request setDidFinishSelector:#selector(gotTheResponse:)];
[networkQueue addOperation:request];
[networkQueue go];
I got mine to work. Hope this helps somebody :)
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
[#"test" stringByAppendingString:#".plist"]];
NSDictionary *plist = [[NSDictionary alloc] initWithContentsOfFile:path];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"yourUrl"]];
[request addData:data withFileName:#"test.plist" andContentType:#"propertylist/plist" forKey:#"file"];
[request setDelegate:self];
[request startAsynchronous];
If you are correct that xml and text files are fine with the above code then the most likely explanation would seem to be that either the path to the plist file is incorrect, or the file permissions don't allow the file to be read.
You can enable debug in ASIHTTPRequestConfig.h that might reveal more about what's going on.