Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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
如何在ios中向AmazonS3服务器发送图像_Ios_Objective C_Amazon S3_Uiimage - Fatal编程技术网

如何在ios中向AmazonS3服务器发送图像

如何在ios中向AmazonS3服务器发送图像,ios,objective-c,amazon-s3,uiimage,Ios,Objective C,Amazon S3,Uiimage,我正在使用下面的代码将图片从gallery发送到ios中的amazon服务器,但没有成功。我可以知道它需要发送的格式吗?我需要只传递文件名还是图像的整个路径 谢谢 NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", @"x-access-token": @"eyJ0

我正在使用下面的代码将图片从gallery发送到ios中的amazon服务器,但没有成功。我可以知道它需要发送的格式吗?我需要只传递文件名还是图像的整个路径

谢谢

NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
                           @"x-access-token": @"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2UxOTAiLCJpc3MiOiJodHRwczovL3d3dy5oY3VlLmNvIiwicGVybWlzc2lvbnMiOiJhdXRoZW50aWNhdGVkIiwidXNlcmlkIjpudWxsLCJpYXQiOjE0ODYyMDczNDB9.ewu2QtoHl1kBSrM1y6yL9Jr58kiGmhCsy2YWoFrE2OU",
                           @"cache-control": @"no-cache",
                            };
NSArray *parameters = @[ @{ @"name": @"username", @"fileName": localFilePath} ];
NSString *boundary = @"----WebKitFormBoundary7MA4YWxkTrZu0gW";

NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
    [body appendFormat:@"--%@\r\n", boundary];
    if (param[@"fileName"]) {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
        [body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
        [body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];
        if (error) {
            NSLog(@"%@", error);
        }
    } else {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
        [body appendFormat:@"%@", param[@"value"]];
    }
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://zambalauploaddev.ap-southeast-1.elasticbeanstalk.com/upload"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);

                                                    NSDictionary * responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                                    NSLog(@"Reponse Dict:%@",responseDict);
                                                }
                                            }];
[dataTask resume];

您必须发送文件数据(NSData)。[body appendData:[[NSString stringWithFormat:@“内容处理:表单数据;名称=\”%@\“文件名=\”%@\“\r\n”、@“abc”、@“image.jpeg”]数据使用编码:NSUTF8StringEncoding];[body appendData:[[NSString stringWithFormat:@“内容类型:%@\r\n\r\n”,mimetype]dataUsingEncoding:NSUTF8StringEncoding]];[正文数据:数据]//这里的数据是图像的nsdata,NSString*mimetype=@“图像/jpeg”;您之所以使用
POST
及其多部分/表单数据的复杂性,是因为您可以简单地
PUT
原始二进制图像内容吗?使用afnetworking上传图像的最佳方式