简体   繁体   English

图像上传使用AFNetworking,ios

[英]image upload using AFNetworking, ios

I have to upload image to server by sending bytes of photo and I'm using AFNetworking, 我必须通过发送照片的字节将图像上传到服务器,我正在使用AFNetworking,
but I keep getting this error- 但我一直得到这个错误 -

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x10abb7350 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 错误:错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(Cocoa错误3840.)”(JSON文本没有以数组或对象开头,并且选项允许未设置片段。)UserInfo = 0x10abb7350 {NSDebugDescription = JSON文本不以数组或对象和选项开头,以允许未设置片段。}

If the PHP server is not responding with application/json , then you have to tell your manager to accept general HTTP responses: 如果PHP服务器没有响应application/json ,那么你必须告诉你的manager接受一般的HTTP响应:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"image" fileName:imageFilename mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Success: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

If you server is responding with JSON response, then perhaps it's not setting the Content-Type header properly, eg, header("Content-type: application/json"); 如果您的服务器正在响应JSON响应,那么它可能没有正确设置Content-Type标头,例如header("Content-type: application/json"); .

Please check Follow the link for best and easy way to image uploading. 请查看关注链接,以获得最佳和最简单的图片上传方式。

https://stackoverflow.com/a/49231873/5247430 https://stackoverflow.com/a/49231873/5247430

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM