开发者

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 se开发者_StackOverflow中文版ndSynchronousRequest: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.

  1. A filename specified using the content disposition header.
  2. The last path component of the URL.
  3. The host of the URL.

The content disposition header would be the best solution so make sure that the server sets it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜