在 iOS 中将文件转换为 Base64 URL 安全编码格式 [英] Converting file to Base64 URL safe encoding format in iOS

查看:178
本文介绍了在 iOS 中将文件转换为 Base64 URL 安全编码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该将文件转换为 Base64 并将该字符串添加到我附加到 URL 的 JSON 中.我拥有的文件是一个 .zip 文件和一个带有 .info 扩展名的文本文件..info 文件正确上传,但使用 .zip 文件时,我收到Incorrect Padding"错误作为响应从服务器.

I am supposed to convert a file in to Base64 and add that String to a JSON which I am appending to a URL. The files that I am having are a .zip file and a text file with .info extension. The .info file is uploading correctly but with .zip file I am getting "Incorrect Padding" error as response from the server.

以下是我的作品;

- (void)uploadingData: (NSString *)fileName {

    NSArray *directoryPathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [directoryPathsArray objectAtIndex:0];

    NSString *absoluteFilePath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, baseDirName, fileName];

    NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:absoluteFilePath];
    [inputStream open];

    uint8_t buffer[1024];

    int len;

    NSMutableString *total = [[NSMutableString alloc] init];

    while ([inputStream hasBytesAvailable]) {
        len = [inputStream read:buffer maxLength:sizeof(buffer)];

        if (len > 0) {
             [total appendString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]];
        }
    }


    NSData *plainData = [total dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];

// Adding to JSON and upload goes here.
}

我哪里做错了?

另外,在将转换后的字符串附加到 JSON 并上传到服务器之前,有没有一种方法可以检查转换后的字符串是否完全符合 Base64?

Additionally, is there a way I can check the converted string is exactly according to Base64, before it append to JSON and upload to server?

谢谢

推荐答案

我已经找到了错误和答案.我不需要将 .zip 文件转换为 NSInputStream 并且我没有将 URL 安全方法应用于编码字符串.以下是我解决它的方法.

I have found the mistakes as well as the answer. I do not need to convert the .zip file in to NSInputStream and I haven't applied URL safe methods to the encoded string. Following is the way I have tackled it.

- (void)uploadingData: (NSString *)fileName {

    NSArray *directoryPathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [directoryPathsArray objectAtIndex:0];

    NSString *absoluteFilePath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, baseDirName, fileName];

    NSData *zipFileData = [NSData dataWithContentsOfFile:absoluteFilePath];

    NSString *base64String = [zipFileData base64EncodedStringWithOptions:0];

    base64String = [base64String stringByReplacingOccurrencesOfString:@"/"
                                                            withString:@"_"];

    base64String = [base64String stringByReplacingOccurrencesOfString:@"+"
                                                           withString:@"-"];

// Adding to JSON and upload goes here.
}

Android 中,有一种方法可以使用单个函数进行 Base64 编码和 URL 安全格式设置.

In Android there is a way to do both Base64 encoding and URL safe formatting with a single function.

byte[] bytes = Files.toByteArray(new File(sFileName));
byte[] encoded = Base64.encodeBase64URLSafe(bytes);

我不知道在 iOS 上是否也有类似的简单方法.

I have no idea whether there is a similar kind of easy way of doing it with iOS too.

这篇关于在 iOS 中将文件转换为 Base64 URL 安全编码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆