1

認証済みのファイルをダウンロードする必要があります。私はこれをpython側に持っています:

@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def downloadData(request):
    schema = request.user.company.username
    db = dbSync(schema)

    filePath = db.dbPath()

    tables = [(db.getTable('location'), 0)]

    if db.copyTables(filePath, tables):
        wrapper = FileWrapper(file(filePath))
        response = HttpResponse(wrapper, content_type='application/x-sqlite3')
        response['Content-Length'] = os.path.getsize(filePath)
        response['Content-Disposition'] = 'attachment; filename="tables.db"'
        response['Content-Transfer-Encoding'] = 'binary'
    else:
        response = HttpResponseNotModified()

    return response

Pythonリクエストライブラリ/ブラウザを使用すると、これはうまく機能します。

それから私はiOSでこれを持っています:

[self.session POST:@"sync/tables/" parameters:tables
           success:^(NSURLSessionDataTask *task, id responseObject) {
               DDLogInfo(@"%@", responseObject);
               handler(nil);
           }
           failure:^(NSURLSessionDataTask *task, NSError *error) {
               DDLogCError(@"%@",error);

              handler(error);
           }];

リクエストはパスしましたが、responseObject はnil.

だから私は試してみます:

NSURL *URL = [NSURL URLWithString:@"http://localhost:8000/sync/tables/"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];

    [request setValue:self.token forHTTPHeaderField:@"AUTHORIZATION"];
    [request setHTTPMethod:@"POST"];

    NSMutableString *params = [NSMutableString string];

    [tables enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [params appendFormat:@"%@=%@", key, obj];
    }];

    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.db"];

        return [NSURL fileURLWithPath:path];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        DDLogInfo(@"File downloaded to: %@", filePath);

        if (error) {
            DDLogError(@"%@", error);
        }
        handler(error);
    }];
    [downloadTask resume];

これはかなりの繰り返しです。そう:

1) NSURLSessionDownloadTask を使用して、パラメーターを送信することは可能self.session POSTですか?

2) またはself.session POST、ファイルを取得しますか?

4

1 に答える 1