0
votes

1) I want to download the data of any kind like the files of type .text, .png, .jpg, .docx, .xls, .pdf, .mp4, or whatever be the kind of files, Then i want to save it to the application sandboxs document directorys any of the sub directories that i have created under document directory of application sandbox.

2) Again whenever the user want to upload the files saved in the subdirectories of the application sandboxs document directory, The user will be able to browse through the data in the different directories of application sandboxs document directory, For that i have listed the data in the subdirectories of document directory of application sandbox in UITableView so that the user should be able to choose any of the file from the particular directory.

Problems/ things where i have stucked

I am using ASIHttpRequest for the upload and download , Where

1) For first need , means for downloading data i am using the methods -(void)grabURLInBackground to download the data from web and if its downloaded successfully then in the method -(void)requestFinished:(ASIHTTPRequest *)request i am saving that data to the subdirectory of the document directory of application sandbox with the particular name. The working code is below

-(void)grabURLInBackground
{
    NSURL *url = [NSURL URLWithString:@"http://wordpress.org/plugins/about/readme.txt"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    NSLog(@"responseString:%@",responseString);

    UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"Download Status" message:@"Download finished" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alt show];

    //Use when fetching binary data
    //NSData *responseData = [request responseData];
    //NSLog(@"responseData:%@",responseData);

//For storing the data to the subdirectory of the document directory named Doc the following code is used.

    NSArray  *paths;
    NSString *documentsDirectory,*docDirectoryPath,*docFilePath;
    //NSString *imageCachePath,*imageDicPath;

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSLog(@"documentsDirectory:%@",documentsDirectory);

    docDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"/Docs"];
    NSLog(@"docDirectoryPath:%@",docDirectoryPath);

    docFilePath = [docDirectoryPath stringByAppendingPathComponent:@"textFileTwo"];
    NSLog(@"docFilePath:%@",docFilePath);

    if (![[NSFileManager defaultManager] fileExistsAtPath:docFilePath])
        [[NSFileManager defaultManager] createFileAtPath:docFilePath
                                                contents:[NSData dataWithContentsOfFile:responseString]
                                              attributes:nil];

//************************************//

Here what i want after the download finishes we have the two option the way to fetch the text data and the way to fetch the binary data, Thats what is the thing , Here in my case the data will be of any kind, And i want to save that to particular directory, I will save it on my own but i want the Unique way to fetch the any kind of data and to save it to particular directory .

//************************************//

}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"error:%@",error);    
}

2) For the 2nd need means for the uploading data to any URL m using the same ASIHttpRequest like

-(void)uploadData {

//Suppose i want to upload the file that i have juz downloaded by the download code above.
// i fetched the path of the file i just saved with download code above, See the code below.

    NSArray  *paths;
    NSString *documentsDirectory,*docDirectoryPath,*docFilePath;
    //NSString *imageCachePath,*imageDicPath;

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSLog(@"documentsDirectory:%@",documentsDirectory);

    docDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"/Docs"];
    NSLog(@"docDirectoryPath:%@",docDirectoryPath);

    docFilePath = [docDirectoryPath stringByAppendingPathComponent:@"textFileTwo"];
    NSLog(@"docFilePath:%@",docFilePath);

// Upload Code

    NSString *strURL = @"http://192.168.1.201/MyLegalNetMobile/MyLegalNetService.svc/FileUpload";

    ASIFormDataRequest *uploadRequest = [ASIFormDataRequest requestWithURL:[NSURL   URLWithString:strURL]]; // Upload a file on disk

    // Upload image data using asihttprequest

    //UIImage *tempImg=[UIImage imageWithContentsOfFile:[NSString stringWithContentsOfURL:[NSURL URLWithString:imageCachePath] encoding:NSUTF8StringEncoding error:nil]];
    //NSData *imageData1=UIImageJPEGRepresentation(tempImg, 1.0);


    NSString *fetchedDataOfTxtFiles = [NSString stringWithContentsOfURL:[NSURL URLWithString:docFilePath] encoding:NSUTF8StringEncoding error:nil];

    NSData *textData = [NSData dataWithContentsOfFile:fetchedDataOfTxtFiles];
    NSLog(@"fetchedDataOfTxtFiles:%@",fetchedDataOfTxtFiles);
    NSLog(@"textData:%@",textData);

    [uploadRequest setData:textData withFileName:@"textFileTrialThree" andContentType:@"txt" forKey:@"txtData"];
    [uploadRequest setRequestMethod:@"POST"];
    [uploadRequest setDelegate:self];
    [uploadRequest setTimeOutSeconds:10.0];
    uploadRequest.shouldAttemptPersistentConnection = NO;
    [uploadRequest setDidFinishSelector:@selector(uploadRequestFinished:)];
    [uploadRequest setDidFailSelector:@selector(uploadRequestFailed:)];
    [uploadRequest startAsynchronous];

//************************************//

Here again i have the different ways to upload the different kind of data, like for uploading the text data, different, ways is there same for the pdf, and image data is also, here i want the unique way to upload any kind of data to server, Also here I tried the image data uploading and text data uploading , Means i uploaded the files that i download from the any url. At the time of saving that downloaded files i converted them to NSData and saved to particular path of application sandboxs belonging directories. So while uploading again i got that path and for image data i converted the nsdata to uiimage , for the text file i only gave the path of file and uploaded the fiels to somewhere , The Files get uploaded on server, but there size was 0 bytes only, and the formate was different.

//************************************//

}

-(void)uploadRequestFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];
    NSLog(@"Upload response %@", responseString);
}

-(void)uploadRequestFailed:(ASIHTTPRequest *)request{
    NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]);
}

// Exact Problem.

/* Any data that we download from the web using ASIHttpRequest before saving it to any path to application sandbox we convert some kind of data to NSData, And it get saved .

On the click of Browse button i have populated the data from different different subdirectories of the Document directory of the application sandbox in the UITableView, So I want to show the names of files with their extensions means with type that files were downloaded [as we save all data with converting to NSData it get saved with the names we give while saving only].

And then the time comes for the users to upload that data to any of the URL at that time also the files should get stored with their original formates means with which we downloaded the, */

2
i want the unique way to save the data of any kind downloaded using the ASIHttpRequest to the any application sandbox's directory, And again i want the Unique way to upload the data of any kind that we stored after download in the application sandbox. Also When we save the data to application sandbox could we save it with their real extension, As at the time of saving the data to application sandbox we convert it to NSData and its saved with the file name that we give.iShwar
ASIHTTPRequest is quite old now, and I don't think it is supported anymore. You are better off using a more modern networking library such as AFNetworking.Abizern
iShwar, it's not clear from your question what your problem is.occulus

2 Answers

0
votes

To get list of files in directory try

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

To get file extension take a look on responce headers. They can contain ContentType which was downloaded.

0
votes

why not use the request property called downloadDestinationPath?? If you use it, you don´t need to do anything in RequestFinished method because the ASIHTTPRequest library keeps the type of the files what you have downloaded.

The request finished method is always for doing something with the data you have downloaded, as parsing an html file for remove the html headers. If you don´t want to modify the file that you are downloading you should use this method for show download status only.

Edit the download path before start the request:

NSArray  *paths;
NSString *documentsDirectory,*docDirectoryPath,*docFilePath;

paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

documentsDirectory = [paths objectAtIndex:0];

docDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"Docs"];// Remove the "/" from the string paths because you are using "stringByAppendingPathComponent"

docFilePath = [docDirectoryPath stringByAppendingPathComponent:@"textFileTwo"];

request = [ASIHTTPRequest requestWithURL:YOUR URL];

[request setDownloadDestinationPath:docFilePath];

[request startAsynchronous];

To list the content:

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error2];
for (int i = 0; i<[directoryContent count]; i++){
     NSLog(@"content == %d", [directoryContent objectAtIndex:i];
}