0

我尝试从服务器下载 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;

我真的需要设置代表我该怎么办?

4

1 回答 1

2

我发现了问题并解决了。这一切都是因为一个简单的if else。在

ASIHTTPRequest.m

-(void)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"]) {

它与委托正常工作,也保存到这个

于 2013-04-15T07:16:08.290 回答