如果文件名带有特殊字符,则不会下载文件

时间:2019-07-12 08:25:01

标签: ios objective-c xcode

文件未下载且未显示任何信息  文件中存在的内容。有时它会显示一条消息,即文件已损坏或文件类型未知。

injurydocObj=[self.injuryDocSN objectAtIndex:indexPath.row];
NSString *path = injurydocObj.athleteDoumentPath;
NSString *fileName=injurydocObj.documentName;

path = [path stringByReplacingOccurrencesOfString:@"\\"
                                     withString:@"/"];
path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *strFileType=@"";
NSArray *arFileName = [fileName componentsSeparatedByString:@"."];
if(arFileName.count>0)
{
    strFileType = arFileName[arFileName.count-1];
    strFileType = [strFileType lowercaseString];
}
if([strFileType isEqual:@"doc"] || [strFileType isEqual:@"docx"] || [strFileType isEqual:@"pdf"]){

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];

}`

请帮助我显示pdf,doc和docx文件类型,即使文件名包含特殊字符或空格

1 个答案:

答案 0 :(得分:0)

使用专用API stringByAddingPercentEncodingWithAllowedCharacters

添加百分比编码
NSString *path = injurydocObj.athleteDoumentPath;
NSString *fileName = injurydocObj.documentName;
path = [[path stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]
        stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];

NSURL *url = [NSURL URLWithString: path];

if ([@[@"doc", @"docx", @"pdf"] containsObject: url.pathExtension]) {

...

NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];