读取 .xlsx 文件 iOS Objective C

Reading .xlsx file iOS Objective C

我正在尝试使用 AFNetworking 从服务器下载 .xlsx 文件。但它给我错误说:

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: application/zip" UserInfo={NSLocalizedDescription=Request failed: unacceptable content-type: application/zip

它显示了 NSError 中的整个响应。

我不想使用任何第三方库,因为我正在将其集成到我的 SDK 中。

在 AFNetworking2.x 下载 .xlsx 文件。您不能使用此代码。此代码用于 Json 序列化。 原因

/* 
In AFURLResponseSerialization.m this line will convert downloaded data to json, which make invalid serialization error 
*/
    responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError];

代码无效 :

    // This code will not work for XLSX File
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

        [manager GET:@"Link to xlsx File" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];

使用这个 :

https://github.com/AFNetworking/AFNetworking/tree/2.x#creating-a-download-task

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"Link to xlsx File"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"File downloaded to: %@", filePath);
    }];
    [downloadTask resume];