0
요즘 AFNetworking (v3.0)을 배우고 있는데,이를 사용하여 node.js + express가 작성한 서버에 이미지를 업로드하려고합니다. *************************************************************************************************************************** " 요청이 잘못 되었습니까? 아니면 문제가 있습니까? 아니면 node.js를 처음 사용하고 표현하는 것입니다. 문제는 유형 문제일까요? 아마도 모르겠지만, 알고 싶다면 ~ 답장 해주세요.AFNetworking을 사용하여 node.js가 서버에 이미지를 업로드하지 못했습니다.
iOS 사이드 :
NSDictionary *params = @{@"enctype": @"multipart/form-data"};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost:8080/images" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *data = UIImagePNGRepresentation(self.imageView.image);
[formData appendPartWithFileData:data name:@"pic" fileName:@"pic.png" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"上传成功");
}
}];
[uploadTask resume];
서버 측 :
router.route('/images').post(multiparty(), function(req, res) {
fs.readFile(req.files.image.path, function(err, data) {
var imageName = req.files.image.name;
if (!imageName) {
console.log("there was an error");
res.json({message:"error", code:500});
} else {
var newPath = __dirname + "/uploads/fullsize/" + imageName;
fs.writeFile(newPath, data, function(err) {
res.json({message: 'successfully upload image!', code:200});
});
}
});
});
당신이 말한 것을 바꾼 후에 잘 작동합니다! 3q ~ 谢谢! –