1

我从重写的 url 下载 pdf 文件,所以我无法从 url 获取文件名。我怎么才能得到它 ?

我当前的代码:

    NSString *documentsDirectory = [NSHomeDirectory()
                                        stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Document.pdf"];
     //url rewrited: downloadUrl = http://mySite/pdf/2020?pointer=451&Entity=4
    NSData *pdfData = [NSData dataWithContentsOfURL:downloadUrl];
    [pdfData writeToFile:filePath atomically:YES];
4

2 回答 2

4

如果你使用NSURLConnection,你可以使用-[NSURLResponse suggestedFilename]来获取文件名。这将考虑到服务器在使用内容处置标头时发送的文件名。

于 2013-04-30T09:49:27.477 回答
0

试试这样:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{


 if (url != nil && [url isFileURL]) {
    DownloadedFileUrl =[NSString stringWithFormat:@"%@",url];
           DownloadedFileUrl = [DownloadedFileUrl stringByReplacingOccurrencesOfString:@"%20"  withString:@" "];
    DownloadedFileUrl = [DownloadedFileUrl stringByReplacingOccurrencesOfString:@"file://localhost"  withString:@""];
    NSLog(@"DownloadedFileUrl:%@",DownloadedFileUrl);
    NSString*str=[NSString stringWithFormat:@"%@",url];
    NSArray *sep = [str componentsSeparatedByString:@"/"];
    NSArray *sep1=  [[sep lastObject]  componentsSeparatedByString:@"."];
 FileNameStr1=[NSString stringWithFormat:@"%@",[sep1 objectAtIndex:0]];


NSLog(@"FileNameStr1:%@",FileNameStr1);
}}
于 2013-04-30T08:19:29.417 回答