将任何类型的文件上传和下载到应用程序沙箱的独特方式

Posted

技术标签:

【中文标题】将任何类型的文件上传和下载到应用程序沙箱的独特方式【英文标题】:Unique way to upload and download any kind of files to application sandbox 【发布时间】:2013-10-03 06:58:58 【问题描述】:

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

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

我遇到的问题/事情

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

1) 对于第一个需要,意味着下载数据我使用方法 -(void)grabURLInBackground 从网络下载数据,如果下载成功,则在方法 -(void)requestFinished:(ASIHTTPRequest *)request i我将该数据保存到具有特定名称的应用程序沙箱的文档目录的子目录中。工作代码如下

-(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,文件也应该以我们下载的原始格式存储,*/

【问题讨论】:

我想要一种独特的方式来将使用 ASIHttpRequest 下载的任何类型的数据保存到任何应用程序沙箱的目录中,我再次想要一种独特的方式来上传我们在下载后存储的任何类型的数据在应用程序沙箱中。此外,当我们将数据保存到应用程序沙箱时,我们可以使用它们的真实扩展名保存它吗?在将数据保存到应用程序沙箱时,我们将其转换为 NSData 并使用我们提供的文件名保存。 ASIHTTPRequest 现在已经很老了,我认为它不再受支持了。您最好使用更现代的网络库,例如 AFNetworking。 iShwar,从你的问题中不清楚你的问题是什么。 【参考方案1】:

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

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

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

【讨论】:

【参考方案2】:

为什么不使用名为 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];

【讨论】:

对不起:NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docFilePath error:&error2];

以上是关于将任何类型的文件上传和下载到应用程序沙箱的独特方式的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Studio 中以编程方式上传和下载 Google Drive 上的任何文件

是否可以通过内置应用程序将文件保存在沙箱之外?

如何在 GCDWebUploader 中限制上传文件类型/格式

将图像从文件系统下载到 kivy 应用程序

将消息从沙箱发送到自定义应用程序

文件下载与上传