0

1)我想下载任何类型的数据,如 .text、.png、.jpg、.docx、.xls、.pdf、.mp4 或任何类型的文件,然后我想保存它到应用程序沙箱的文档目录中我在应用程序沙箱的文档目录下创建的任何子目录。

2)再次,每当用户想要上传保存在应用程序沙箱文档目录的子目录中的文件时,用户将能够浏览应用程序沙箱文档目录的不同目录中的数据,为此我列出了数据UITableView 中应用程序沙箱的文档目录的子目录,以便用户应该能够从特定目录中选择任何文件。

我遇到的问题/事情

我正在使用 ASIHttpRequest 进行上传和下载,在哪里

1)对于第一个需要,用于下载数据的手段我使用方法 -(void)grabURLInBackground 从网络下载数据,如果下载成功,则在方法 -(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)对于第二个需要手段,使用相同的 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 中应用程序沙箱的 Document 目录的不同不同子目录填充了数据,因此我想显示文件的名称及其扩展名,这意味着文件已下载的类型 [当我们保存时所有转换为 NSData 的数据都会以我们在保存时给出的名称保存]。

然后是时候让用户将这些数据上传到当时的任何 URL,文件也应该以我们下载的原始格式存储,*/

4

2 回答 2

0

为什么不使用名为 downloadDestinationPath 的请求属性?如果你使用它,你不需要在 RequestFinished 方法中做任何事情,因为 ASIHTTPRequest 库会保存你下载的文件的类型。

请求完成方法始终用于处理您下载的数据,如解析 html 文件以删除 html 标头。如果您不想修改正在下载的文件,您应该使用此方法仅显示下载状态。

在开始请求之前编辑下载路径:

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];

列出内容:

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error2];
for (int i = 0; i<[directoryContent count]; i++){
     NSLog(@"content == %d", [directoryContent objectAtIndex:i];
}
于 2013-10-03T10:10:43.190 回答
0

要获取目录中的文件列表,请尝试

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)路径错误:(NSError **)错误

要获取文件扩展名,请查看响应标题。它们可以包含已下载的 ContentType。

于 2013-10-03T10:02:10.250 回答