Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将任何类型的文件上载和下载到应用程序沙箱的独特方式_Iphone_Ios_Objective C_Ipad_Asihttprequest - Fatal编程技术网

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

Iphone 将任何类型的文件上载和下载到应用程序沙箱的独特方式,iphone,ios,objective-c,ipad,asihttprequest,Iphone,Ios,Objective C,Ipad,Asihttprequest,1) 我想下载任何类型的数据,如.text、.png、.jpg、.docx、.xls、.pdf、.mp4或任何类型的文件,然后我想将其保存到应用程序沙盒的文档目录,即我在应用程序沙盒的文档目录下创建的任何子目录 2) 同样,每当用户想要上传保存在应用程序沙盒文档目录子目录中的文件时,用户将能够浏览应用程序沙盒文档目录不同目录中的数据,为此,我在UITableView中列出了应用程序沙盒的document目录的子目录中的数据,以便用户能够从特定目录中选择任何文件 我结巴的问题/事情 我正在使用AS

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

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

我结巴的问题/事情

我正在使用ASIHttpRequest进行上传和下载,其中

1) 对于第一种需要,下载数据的方法我使用方法-(void)grabURLInBackground从web下载数据,如果数据下载成功,则使用方法-(void)requestFinished:(ASIHTTPRequest*)请求我正在使用特定名称将该数据保存到应用程序沙箱的文档目录的子目录中。工作代码如下

-(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

-(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的时候到了。同时,文件应该以其原始格式存储。我们使用这种方式下载,*/

以获取目录中的文件列表

-(NSArray*)目录路径的内容:(NSString*)路径错误:(NSError**)错误
要获取文件扩展名,请查看Response头。它们可以包含已下载的ContentType。

为什么不使用名为downloadDestinationPath的请求属性?如果使用它,则不需要在RequestFinished方法中执行任何操作,因为ASIHTTPRequest库保留已下载文件的类型

request finished方法始终用于对已下载的数据执行某些操作,例如解析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]目录目录路径:路径错误:&error2];

对于(int i=0;ii希望以独特的方式将使用ASIHttpRequest下载的任何类型的数据保存到任何应用程序沙盒的目录中,并且再次希望以独特的方式上载下载后存储在应用程序沙盒中的任何类型的数据。此外,当我们将数据保存到应用程序沙盒中时,我们是否可以将其与实际数据一起保存xtension,在将数据保存到应用程序沙盒时,我们将其转换为NSData,并使用我们提供的文件名保存。ASIHTTPRequest现在已经很旧了,我认为不再支持它。您最好使用更现代的网络库,如AFNetworking.iShwar,您的问题不清楚您的问题是什么em是。抱歉:NSArray*directoryContent=[[NSFileManager defaultManager]contentsOfDirectoryAtPath:docFilePath错误:&error2];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error2];
for (int i = 0; i<[directoryContent count]; i++){
     NSLog(@"content == %d", [directoryContent objectAtIndex:i];
}