0

我必须从 iPhone 库中选择图像或视频并将这些选定的图像和视频上传到服务器。我查找了多部分表单上传,但无法获得必要的信息。

我有以下 JSON 结构要发布。

{"uuid":"a6059eb6-2417-4575-8f83-e5eca065a1bb","id":901,"username":"somename","description":"Some Desciption","date":"Some date","title":"Some Title","published":1,"type":"Some Type","responsible":["Person 1","Person 2","Person 3"],"products_List":["Product 1"],"assets":[{"uuid":"e1102eae-987a-4930-96ad-5ae331d785bc","fileExtension":"jpg","mimeType":"image\/jpeg","type":"image"},{"uuid":"c61bcc45-5347-4e98-9990-bc949dad24fa","fileExtension":"mp4","mimeType":"video\/mp4","type":"video"}]}

4

2 回答 2

0

试试这个代码,

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.client = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];

NSString *tempFileString = [NSTemporaryDirectory() stringByAppendingPathComponent:@"your-app-temp"];
NSURL *filePathtemp = [NSURL fileURLWithPath:tempFileString];

NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://domain/path" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
     [formData appendPartWithFileData:imageData name:@"imageData" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
} error:nil];

[[AFJSONRequestSerializer  serializer] requestWithMultipartFormRequest:request writingStreamContentsToFile:filePathtemp completionHandler:^(NSError *error) {
    NSURLSessionUploadTask *uploadTask = [self.client uploadTaskWithRequest:request fromFile:filePathtemp progress:nil completionHandler:completionHandler];
    [uploadTask resume];
}];
于 2016-04-25T09:13:51.453 回答
0

您必须使用 AFNetworing 将图像作为多部分数据单独上传。像这样:-

- (AFHTTPRequestOperation*)POSTAction:(APIRestAction)task
            constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
                              success:(void (^)(AFHTTPRequestOperation* operation, id responseObject))success
                              failure:(void (^)(AFHTTPRequestOperation* operation, NSError* error))failure;

此处:- 在 APIRestAction 中设置您的服务器路径。

于 2016-04-25T09:19:17.990 回答