In my iOS app I want to upload an image file and some other parameters through API. image file contains multiple images.
You can use custom image pickers like ELCImagePickerController
There is also some other library that can be used..
WSAssetPickerController
QBImagePickerController
These allow you to pick multiple images. let me know how things go
By Using AFNetworking You can upload multiple image as below code, download AFNetworking
Other Parameter :
NSDictionary *parametersAll = #{#"Value": #"Key"};
NSArray *imageArray;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseUrl: my_url ];
NSMutableRequest *request = [httpClient multipartFormRequestWithMethod:#"POST" path:nil
parameters:parametersAll constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
for(UIImage *img in imageArray)
{
[formData appendPartWithFileData: my_imageData name:#"image" fileName:#"myImage.jpg" mimeType:#"image/jpeg"];
}
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request];
[operation start];
Related
I would like to upload pictures, probably ranging from a few hundred to 1000, originally using AFNetwoking methods, but there is a drawback, it reads all image data one time, then memory is up,for example:
NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[operationQueue setMaxConcurrentOperationCount:1];
for (NSURL *fileURL in filesToUpload) {
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:#"POST"
URLString:#"http://example.com/upload" parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:fileURL name:#"images[]" error:nil];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operationQueue addOperation:operation];
}
this will read all pictures's data before upload, how can i create a queue reduced memory consumption
I want to create method in that coming images and all of them downloading in queue one by one. I use AFNetworking 2.0. I want to know is there any pre-created code that can help me in my question ?
My code
NSString *urlString = link;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
UIImage *image = responseObject;
//here I seve image to disk and so on
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError(#"FAIL download image: %#",error);
}];
Try with dispatch_group: all downloads are group into one queue and are downloaded one by one
AFNetworking 2.0 download multiple images with completion
I want to upload video to web service along with some other parameters. I want to upload userID, videoID and video to web service. While uploading, all the parameters other than video is being sent to web service. I've checked at web service end, and the video is not coming with the request. I am using the following code.
- (void)uploadVideoAtLocalPath:(NSString *)videoPath videoID:(NSString *)videoID userID:(NSString *)userID {
NSString *strServerURL = #"www.mysite.com/user/uploadVideo";
NSURL *URL = [NSURL URLWithString:strServerURL];
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:URL];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:#"POST" path:#"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
// userID
NSData *userIDData = [userID dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:userIDData name:#"userID"];
// videoID
NSData *videoIDData = [videoID dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:videoIDData name:#"videoID"];
// video
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
[formData appendPartWithFileData:videoData name:#"video" fileName:#"video.mov" mimeType:#"video/quicktime"];
}];
[request setURL:URL];
[request setTimeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[AFHTTPRequestOperation addAcceptableStatusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(100, 500)]];
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response String: %#", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", error);
}];
[client enqueueHTTPRequestOperation:operation];
}
Could anyone let me know whether I am doing it correct? If not, could anyone please tell me how to upload video to web service along with other parameters?
Thanks Everyone!
I'm not well in this method. I had the same issue. But, i have fixed it like mixing of POST & GET methods. I just sent my parameters as GET method like below -
NSString *strServerURL = [NSString stringWithFormat:#"www.mysite.com/user/uploadVideo&userID=%d&videoID=%d", 1, 55];
and, sent my video data in POST method as per your method -
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:#"POST" path:#"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
// video
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
[formData appendPartWithFileData:videoData name:#"video" fileName:#"video.mov" mimeType:#"video/quicktime"];
}];
You better try to modify your webservice and try like above way. It should works.
Cheers!
NSURL *url = [NSURL URLWithString:#"http://127.0.0.1:8000/photo/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *headers = [NSDictionary dictionaryWithObject:data forKey:#"attachment"];
//NSLog(#"strimng");
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:#"POST" path:#"upload/" parameters:nil constructingBodyWithBlock: ^(id<AFMultipartFormData> formData) {
NSLog(#"strimng");
[formData appendPartWithFileData:data name:#"attachment" fileName:#"attachment.jpg" mimeType:#"image/jpeg"];
NSLog(#"strimng");
}];
NSLog(#"strimng");
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(#"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
I am Trying to upload the image from the iphone using AFNetworking, but this is not working.
The NSLog just before formdata appenddata command is logged out. But the one after that doesnt seem to. I have also checked if NSdata is nil and thats also not the case. And obv the request is not being sent.
Please can anyone help me.
Assuming that you have to upload an image, why are you creating an NSDictionary with the data? It is not necessary, you just need to use data inside multipartFormRequestWithMethod.
Try the following
// Get the image that you want to upload via ImagePicker or a #property
UIImage *imageIWantToUpload = self.image;
// Create the NSData object for the upload process
NSData *dataToUpload = UIImageJPEGRepresentation(imageIWantToUpload, 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:#"POST" path:#"upload/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:dataToUpload name:#"attachment" fileName:#"attachment.jpg" mimeType:#"image/jpg"];
}];
// You can add then the progressBlock and the completionBlock
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:progress];
[operation setCompletionBlockWithSuccess:success failure:failure];
You should also double check if your server side expects some parameters, because you are setting the NSMutableURLRequest parameters' field to nil.
I need to send to a web server some informations that the user inserts in my app
I was using ASIHTTPRequest but i'm having some problems with iOS 5, so I decided to move to AFNetworking
this is the code I've written so far...
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://theserver.com/upload.php"]];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:[fieldName text] forKey:#"name"];
[parameters setObject:[fieldSurname text] forKey:#"surname"];
Now I need to add a NSData object containing an image, and it should be referenced by forKey:#"attachment"
what should I write? with ASIHTTPRequest I was using
[request setData:myNSData withFileName:#"image.jpg" andContentType:#"application/octet-stream" forKey:#"attachment"];
but with AFNetworking I don't know what should I write, and the examples doesn't help me
thank you very much!
You need to use "multipartFormRequestWithMethod" - eg:
NSMutableURLRequest *request = [[AFHTTPClient sharedClient] multipartFormRequestWithMethod:#"POST" path:#"/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:data mimeType:#"image/jpeg" name:#"attachment"];
}];