首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果文件名包含特殊字符,则无法下载文件

如果文件名包含特殊字符,则无法下载文件
EN

Stack Overflow用户
提问于 2019-07-12 16:25:02
回答 1查看 84关注 0票数 0

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

代码语言:javascript
运行
复制
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文件类型,即使文件名包含特殊字符或空格

EN

回答 1

Stack Overflow用户

发布于 2019-07-12 18:02:59

使用专用接口stringByAddingPercentEncodingWithAllowedCharacters添加百分比编码

代码语言:javascript
运行
复制
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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57003032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档