Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:03:12+00:00 2026-05-25T16:03:12+00:00

How to download docx , pdf , image , pptx , or any file

  • 0

How to download docx , pdf , image , pptx , or any file from a site which ask for credentials for authentication i tried passing the credentials and still data in the nsdata is something else but it store that detain the file created locally can anybody help in the code for downloading any kind of file.

the code is as follows:
in this buttonClicked is called from other file
DownloadingFile.h

    #import "MyAppDelegate.h"

@interface DownloadingFile : NSObject
{
    NSMutableData *webData;
    NSMutableString *soapResults;
    NSURLConnection *conn;
    BOOL *elementFound; 
    BOOL isDoneParsing;
    MyAppDelegate *mydelegate;

    NSString *userCd,*passWord,*siteUrl;

}
@property (nonatomic ,retain) MyAppDelegate *mydelegate;
-(void)buttonClicked;
-(bool)getIsDone;
@end

DownloadingFile.m

#import "DownloadingFile.h"
#import "iExploreAppDelegate.h"

@implementation DownloadingFile

@synthesize mydelegate;

- (void)buttonClicked
{
    mydelegate=(MyAppDelegate *)[[UIApplication sharedApplication] delegate];

    userCd=[[NSString alloc] initWithString:[mydelegate getUserId]];
    passWord=[[NSString alloc] initWithString:[mydelegate getPassword]];

    NSLog(@"In Downloading; ");

    NSURL *url =[NSURL URLWithString:[@"http://abcdef.xyz.com/Docs/NewFolder/myFile.docx" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
        [req setValue:[NSString stringWithFormat:@"bytes=%ld-",0] forHTTPHeaderField:@"Range"];
        [req addValue: @"docx" forHTTPHeaderField:@"Content-Type"];
        [req setHTTPMethod:@"POST"];

      //---set the headers---
   conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    if (conn) {
        NSLog(@"connection done ");

       webData = [[NSMutableData data] init];
    }   
} 


-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;

      newCredential=[NSURLCredential credentialWithUser:userCd password:passWord persistence:NSURLCredentialPersistenceNone];
      NSLog(@"Crediantials done ");
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        NSError* error = [NSError errorWithDomain:@"SoapRequest" code:403 userInfo: [NSDictionary dictionaryWithObjectsAndKeys: @"Could not authenticate this request", NSLocalizedDescriptionKey,nil]];
        NSLog(@"Credentials are not valid");
        [mydelegate loginFailled:false];
    }
}



-(void) connection:(NSURLConnection *) connection 
didReceiveResponse:(NSURLResponse *) response {
    //[webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection 
    didReceiveData:(NSData *) data {
    NSLog(@"recevied data %@",data);
    webData=[NSMutableData dataWithData:data];
    [webData appendData:data];
}

-(void) connection:(NSURLConnection *) connection 
  didFailWithError:(NSError *) error {
    [webData release];
    [connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {

    NSLog(@"Did Finish Loading done ");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"FileName.docx"];

    [webData writeToFile:pdfPath atomically:YES];

    [connection release];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:03:13+00:00Added an answer on May 25, 2026 at 4:03 pm

    I just wrote a quick app to test using Apple’s NSURLConnection which is both a simple and robust solution. I downloaded a few hundred KB Word and PowerPoint files. For authentication I used .htaccess. Works like a charm. Here is the relevant code.

    - (IBAction)clickedDownload:(id)sender {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myserver.com/downloadTest/testfile.pptx"]];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
        [connection start];
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
       // inform the user
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
        if ([challenge previousFailureCount] == 0) {        
            NSURLCredential *newCredential;
            newCredential = [NSURLCredential credentialWithUser:@"theUsername"
                                                       password:@"thePassword"
                                                    persistence:NSURLCredentialPersistenceNone];
            [[challenge sender] useCredential:newCredential
                   forAuthenticationChallenge:challenge];
        } else {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
            // inform the user that password is incorrect
        }
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        receivedData = [[NSMutableData alloc] init];
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [receivedData appendData:data]; 
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSURL *docDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        NSURL *filePath = [docDirectory URLByAppendingPathComponent:@"testfile.pptx"];
        [receivedData writeToURL:filePath atomically:YES];
        [receivedData release];
        connection = nil;
    }
    

    Now I opened the files in ~/Library/Application Support/iPhone Simulator/4.3.2/Applications/[unique-application-code]/Documents– works great!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I download one program that read file and then parse double values from String
I download an image from an URL asynchronously using WebRequest this way: public void
Download JQGrid js file from http://www.trirand.com/blog/ . Is it free? What is this http://www.trirand.net/demoaspnetmvc.aspx
In C# I've to write code for Docx file send from server application to
I have a piece of code that allow users download file from server (document
I download GO compiler for windows from http://code.google.com/p/gomingw/downloads/list . However, in Read Me file,
I download the clojure 1.2 and clojure-contrib-1.2.0.jar from the download site . And I
i download a kml file : <?xml version=1.0 encoding=UTF-8?> <kml xmlns=http://www.opengis.net/kml/2.2> <Document> <Style id=transGreenPoly>
Where I can download sample database which can be used for data warehouse creation?
I download a bz2 file using Python. Then I want to unpack the archive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.