Swati
Swati

Reputation: 2918

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?

Upvotes: 0

Views: 1816

Answers (3)

Huy
Huy

Reputation: 45

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

Upvotes: 2

Swati
Swati

Reputation: 2918

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

Upvotes: 2

JosephH
JosephH

Reputation: 37495

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.

Upvotes: 0

Related Questions