委托设置时,ASIHTTPRequest不保存pdf文件

时间:2013-04-14 15:40:38

标签: ios pdf delegates asihttprequest

我尝试从服务器下载pdf,这是我的代码

ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[GlobalMethod convertURL:[NSString stringWithFormat:@"%@%@",SITE_IMAGE_URL,pview.nameLable.text]]]];
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // Add your filename to the directory to create your saved pdf location
    NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"test.pdf"];

    // TEMPORARY PDF PATH
    // Get the Caches directory
    NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // Add your filename to the directory to create your temp pdf location
    NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:@"test.pdf"];

    req.shouldContinueWhenAppEntersBackground = YES;
    req.delegate = self;
    req.downloadProgressDelegate = pview.progressView;
    [req setDidFinishSelector:@selector(requestDone:)];
    [req setDidFailSelector:@selector(requestWentWrong:)];
    [req setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:pview.nameLable.text,REQ_FILE_NAME, nil]];
    [req setTemporaryFileDownloadPath:tempPdfLocation];
    [req setDownloadDestinationPath:pdfLocation];
    [req startAsynchronous];

当委托设置为self时,它没有将文件保存到目标路径,但是当我对此行进行注释时它正常工作

req.delegate = self;

我真的需要设置委托 我该怎么办?

1 个答案:

答案 0 :(得分:2)

我找到了问题并解决了它。这一切都发生在一个简单的if else。在

  

ASIHTTPRequest.m

in

  

- (无效)handleBytesAvailable

中包含此代码的方法
// Does the delegate want to handle the data manually?
        if (dataWillBeHandledExternally) {

            NSData *data = nil;
            if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) {
                data = inflatedData;
            } else {
                data = [NSData dataWithBytes:buffer length:bytesRead];
            }
            [self performSelectorOnMainThread:@selector(passOnReceivedData:) withObject:data waitUntilDone:[NSThread isMainThread]];

        // Are we downloading to a file?
        }else if ([self downloadDestinationPath]) {
            BOOL append = NO;
            if (![self fileDownloadOutputStream]) {
                if (![self temporaryFileDownloadPath]) {
                    [self setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]];
                } else if ([self allowResumeForFileDownloads] && [[self requestHeaders] objectForKey:@"Range"]) {

在第二个if之前删除else并将其更改为

// Does the delegate want to handle the data manually?
            if (dataWillBeHandledExternally) {

                NSData *data = nil;
                if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) {
                    data = inflatedData;
                } else {
                    data = [NSData dataWithBytes:buffer length:bytesRead];
                }
                [self performSelectorOnMainThread:@selector(passOnReceivedData:) withObject:data waitUntilDone:[NSThread isMainThread]];

            // Are we downloading to a file?
            }if ([self downloadDestinationPath]) {
                BOOL append = NO;
                if (![self fileDownloadOutputStream]) {
                    if (![self temporaryFileDownloadPath]) {
                        [self setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]];
                    } else if ([self allowResumeForFileDownloads] && [[self requestHeaders] objectForKey:@"Range"]) {

它与委托一起正常工作,并保存到此