0

1) .text、.png、.jpg、.docx、.xls、.pdf、.mp4 など、あらゆる種類のファイルをダウンロードしたい。それから保存したい。アプリケーションサンドボックスのドキュメントディレクトリの下に作成したサブディレクトリのいずれかをアプリケーションサンドボックスのドキュメントディレクトリに追加します。

2) 繰り返しになりますが、ユーザーがアプリケーション サンドボックス ドキュメント ディレクトリのサブディレクトリに保存されているファイルをアップロードしたいときはいつでも、ユーザーはアプリケーション サンドボックス ドキュメント ディレクトリのさまざまなディレクトリにあるデータを参照できます。ユーザーが特定のディレクトリから任意のファイルを選択できるように、UITableView のアプリケーション サンドボックスのドキュメント ディレクトリのサブディレクトリ。

問題/私が立ち往生しているもの

アップロードとダウンロードに ASIHttpRequest を使用しています。

1)最初の必要性については、データをダウンロードするための手段 -(void)grabURLInBackground メソッドを使用して Web からデータをダウンロードし、正常にダウンロードされた場合はメソッド -(void)requestFinished:(ASIHTTPRequest *)request で保存していますアプリケーション サンドボックスのドキュメント ディレクトリのサブディレクトリに特定の名前でデータを格納します。作業コードは以下のとおりです

-(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) 2 番目の必要性については、同じ ASIHttpRequest を使用して任意の URL m にデータをアップロードするための手段

-(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]);
}

// 正確な問題。

/* アプリケーション サンドボックスへの任意のパスに保存する前に ASIHttpRequest を使用して Web からダウンロードしたデータは、何らかのデータを NSData に変換し、保存されます。

[参照] ボタンをクリックすると、UITableView のアプリケーション サンドボックスのドキュメント ディレクトリのさまざまなサブディレクトリからデータが取り込まれました。そのため、ファイルの名前を拡張子付きで表示したいのは、ファイルがダウンロードされたタイプを意味します [保存時にNSData に変換されたすべてのデータは、保存時に指定した名前で保存されます]。

そして、ユーザーがそのデータをいずれかの URL にアップロードするときが来ます。また、ファイルは、ダウンロードした元の形式で保存される必要があります。*/

4

2 に答える 2

0

ディレクトリ内のファイルのリストを取得するには、試してください

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)パス エラー:(NSError **)エラー

ファイル拡張子を取得するには、応答ヘッダーを見てください。ダウンロードされた ContentType を含めることができます。

于 2013-10-03T10:02:10.250 に答える