Reputation: 135
I try to download a pdf from server and this is my code
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];
it didn't save file to the destination path when the delegate set to self but it work correctly when I comment this line
req.delegate = self;
I really need the delegate to be set what should I do ?
Upvotes: 0
Views: 126
Reputation: 135
I found the problem and solve it. It's all happened because of a simple if else. In
ASIHTTPRequest.m
in
-(void)handleBytesAvailable
method you have this code in
// 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"]) {
when you delete the else before the second if and change it to
// 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"]) {
it work correctly with delegate and also save to this
Upvotes: 2