gpt4 book ai didi

ios - 使用 NSURLSessionDownloadTask 从重定向 URL 下载 pdf 文件

转载 作者:行者123 更新时间:2023-11-28 21:54:41 24 4
gpt4 key购买 nike

我正在制作一个应用程序,用户可以在其中登录网站并下载一些 pdf 文件。

我能够使用 URLSession 成功登录该站点。

现在我的问题是文件的下载。

该网站使用带有以下“http://www.someurl.com/getfile.asp?FILE=12345”的网址这显然是对嵌入在获取 url 中的实际 pdf 文件的重定向,

尝试将 NSURLSessionDownloadTask 与 url 一起使用,但无法下载 pdf 文件。

对于如何从响应中获取实际 URL 并下载文件有什么帮助吗?

这里是我尝试过的:注意我更改了 url 只是为了问这个问题

//1 create NSURL to the login page
NSURL *tempURL = [NSURL URLWithString:@"http://www.someurl.com/login.asp"];
// 2 create Mutable request with header and username and password
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:tempURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = @"userid=abcdef&passwd=123456789&submit=Login";

NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
//3 Create NSURLSession to login
NSURLSession *session = [NSURLSession sharedSession];
//4 Create Session Data task to download the html page required
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{

NSURL *fileURL = [NSURL URLWithString:@"http://www.someurl.com/file.asp?File=20904"];

NSURLSessionDownloadTask *pdfDownload = [[NSURLSession sharedSession] downloadTaskWithURL:fileURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

NSLog(@"%@", location.description);

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error1;

//getting application's document directory path
NSArray * tempArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [tempArray objectAtIndex:0];

//adding a new folder to the documents directory path
NSString *appDir = [docsDir stringByAppendingPathComponent:@"/PDFs/"];

//Checking for directory existence and creating if not already exists
if(![fileManager fileExistsAtPath:appDir])
{
[fileManager createDirectoryAtPath:appDir withIntermediateDirectories:NO attributes:nil error:&error1];
}

//retrieving the filename from the response and appending it again to the path
//this path "appDir" will be used as the target path
appDir = [appDir stringByAppendingFormat:@"/%@",[[pdfDownload response] suggestedFilename]];

//checking for file existence and deleting if already present.
if([fileManager fileExistsAtPath:appDir])
{
NSLog([fileManager removeItemAtPath:appDir error:&error1]?@"deleted":@"not deleted");
}

//moving the file from temp location to app's own directory
BOOL fileCopied = [fileManager moveItemAtPath:[location path] toPath:appDir error:&error1];
NSLog(fileCopied ? @"Yes" : @"No");
NSLog(@"%@",appDir);
}];

[pdfDownload resume];

}];
[postDataTask resume];

}

当我记录这个时,我得到空值NSLog(@"%@",appDir);

最佳答案

您必须将路径组件附加到目录,而不是格式。试试这个

appDir = [appDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[response suggestedFilename]]];

请记住,在使用 stringByAppendingPathComponent:

时不需要使用“/”

关于ios - 使用 NSURLSessionDownloadTask 从重定向 URL 下载 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26973927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com