<form method="post" action="upload.php?u=test&p=test" name="FrmUpload" autocomplete="off" enctype="multipart/form-data">
<input type="file" name="file" id="file" onchange="document.forms[0].submit();"/>
</form>
The above code is working in html. I am trying to do in AFnetworking multipart audio file uploading: This is my equivalent ios code:
NSDictionary *dic = #{#"id":#"file"};
[[RequestOperationManager sharedManager] POST:#"my url string" parameters:dic
constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
NSError *error = nil;
[formData appendPartWithFileURL:[NSURL fileURLWithPath:#"my file path"] name:#"file" fileName:file mimeType:#"audio/aac" error:&error];
}success:^(AFHTTPRequestOperation *operation, id responseObject){
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(#"%d : %#", error.code, error.description);
}
}];
It returns the 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.)
-(void)uploadAudio{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] *
initWithBaseURL:[NSURL URLWithString:#"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:#"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
i Think Some detail must be missing ... like ur url ......or key.....
Related
I have string objects and image stored in nsdata. How can i send it to a server by accessing the url? I have seen examples .but its not working.Can someone tell me
Thanks in Advance!!
You could use AFNetworking to easily send string and image to server using url.
Here is a link of tutorial how to use AFNetworking framework.
AFNetworking Tutorial
AFNetworking is the best way to do that. and there are so much tutorials out there.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"username": _username.text,
#"password": passwordMD5
};
[manager POST:#"http://example.com/ws/test.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject); //This is the Response
}else{
//if you didnt get expected output then handle it here
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
//Handle any network errors here. ex: if internet disconnected.
}];
Here is an example how to upload an image with string objects as a dictionary to a server using AFNetworking.
- (void)uploadPhoto {
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:#"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
I am totally new to iOS.I want to upload images to my server using AFNetworking.
I tried it but it still not done. Please Help me, thanks in advance
try with this its work for me
-(void) uplodeImages{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"url where to upload images"]];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:#"cat1.png"]);
NSDictionary *parameters = #{#"username": #"", #"password" : #""};
AFHTTPRequestOperation *op = [manager POST:#"cat1.png" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:#"userfile" fileName:#"cat1.png" mimeType:#"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
I am struggling for hours to get AFMultipartFormData to work...
I need to signup a user by sending his info and his picture.
The documentation i have from the back-end developer to achieve this is:
The required parts are: 'registration' with content type 'application/json' and 'pic' with content type image/*
I tried several ways to do this without any success. What is wrong with the code below?
-(void)serverAuthentication {
NSDictionary *jsonDictionary = #{#"clientId":#"someClientId3", #"name":#"Pantelis", #"email":#"someuser#gmail.com", #"password":#"1234567"};
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"sampleJohnTsioris"], 0.5);
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];
if (!jsonData) {
NSLog(#"JSON ERROR");
} else {
NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(#"JSON OUTPUT: %#",JSONString);
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[manager POST:#"http://someserver.com/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:jsonData name:#"registration"];
[formData appendPartWithFileData:imageData name:#"pic" fileName:#"photo.jpg" mimeType:#"image/*"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"operation=%#",operation);
NSLog(#"Response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"operation=%#",operation);
NSLog(#"Error: %#", error);
}];
}
The error i get:
Error Domain=com.alamofire.error.serialization.response Code=-1011
"Request failed: unsupported media type (415)"
You can't change the content type of the request. It must be "multipart/form-data".
You need to make sure the "registration" part has a mime type of "application/json".
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://someserver.com/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:jsonData name:#"registration" fileName:#"user.json" mimeType:#"application/json"];
[formData appendPartWithFileData:imageData name:#"pic" fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"operation=%#",operation);
NSLog(#"Response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"operation=%#",operation);
NSLog(#"Error: %#", error);
}];
I found out the solution. As Jeffery Thomas wrote, you can't change the content type of the request, and there was the problem. Specifically, there was no problem at all sending the image but the json object which needed the name "registration" AND the content-type and i couldn't find the proper way to pass both. So, I made up the headers needed to do that (pass the content-type and the name).
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://vext.dsigned.gr:8080/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys: #"application/json; charset=UTF-8",#"Content-Type", #"form-data; name=\"registration\"",#"Content-Disposition", nil];
[formData appendPartWithHeaders:headers body:jsonData];
[formData appendPartWithFileData:imageData name:#"pic" fileName:#"avatar.png" mimeType:#"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"operation=%#",operation);
NSLog(#"Response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"operation=%#",operation);
NSLog(#"Error: %#", error);
}];
I am trying to upload a picture from an iPhone to a Django Web Service and I keep getting a 400 with this specific message :
error: {"image": ["No file was submitted. Check the encoding type on the form."]}.
I have sat up for hours trying to figure this out and i believe that somehow, the mime-type is not being sent properly.
Would you have any ideas, or better ways to upload an image than my code below :
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://MYADDRESS"]];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"user" password:#"pass"];
NSData *imageData = UIImageJPEGRepresentation(image, 1);
NSDictionary *parameters = #{#"image" : imageData, #"name" : #"MyImage" , #"Content-Type" : #"image/jpeg"};
AFHTTPRequestOperation *op = [manager POST:#"/api/photo/" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:#"MyImage" fileName:#"MyImage.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(#"%f%% Uploaded", round(((float)totalBytesWritten / (float)totalBytesExpectedToWrite) * 100.));
}];
[op start];
I'm banging my head against the wall with this one. I want to select UIImage from library and upload it to server, like on could do with <form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">. Instead of success I got 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=0x145e5d90 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I tried this way:
-(void)uploadPhoto{
NSString *path = #"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password, #"private" : #(priv), #"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(self.imageView.image){
[formData appendPartWithFileData:imageData name:#"avatar" fileName:#"avatar.jpg" mimeType:#"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"[UploadVC] success = %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[UploadVC] error = %#", error);
}];
[self blockView:self.view block:YES];
}
but it's not working... server says that there is no file. Not sure if encrypting is wrong, mime type or what?
Tried also this:
[manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"[UploadVC] success = %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[UploadVC] error = %#", error);
}];
and this:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:imageData name:#"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"[UploadVC] success = %#", responseObject);
[self blockView:self.view block:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[UploadVC] error response.object = %#", operation.responseObject);
[self blockView:self.view block:NO];
}];
nothing is working. Hope someone can help, 'cause I'm stuck with it and circling from question to question here on SO
tia
EDIT: new attempt
1) first was multi-part form
2) creating upload task
none of them worked for me, so I'm still trying to cope with that, but cannot see any solution
I'm not sure which part (I think that some details were missing) was responsible, but I did it finally :) here you go:
-(void)uploadPhoto{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:#"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
Works like a charm :)
You can upload an image with AFNetworking using Swift like this...
let compression = 0.5
let imageData = UIImageJPEGRepresentation("image", CGFloat(compression))
if imageData != nil{
var manager = AFHTTPRequestOperationManager()
manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html", "application/json"]) as Set<NSObject>
var dictParams = [
"familyId":"10000",
"contentBody" : "Some body content for the test application",
"name" : "the name/title",
"typeOfContent":"photo"
]
let url = "http://...."
manager.POST(url, parameters: dictParams, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
//code
formData.appendPartWithFileData(imageData, name: "file", fileName: "filename", mimeType: "image/png")
}, success: { (operation:AFHTTPRequestOperation!, responseObject:AnyObject!) -> Void in
println(responseObject)
}, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in
println(error)
})
}
UIImage *image = [UIImage imageNamed:#"decline_clicked.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);
NSString *queryStringss = [NSString stringWithFormat:#"http://your server/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imageData name:#"fileName" fileName:#"decline_clicked.png" mimeType:#"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *dict = [responseObject objectForKey:#"Result"];
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];