0

first of all I am sorry for this kind of questions, I am a little confused with Objective C, the function I am trying to write is to upload image to a server using web service, I have already dine it using C# but it I cann't figuir out what is the problem in my objective-C code

the C# code is:

using(var wb=new WebClient){
var Data=new NameValueCollection();
data["image"]=base64(fileName)/*base64 is a function to convert the image with"file name" to base64 string*/
Uri myUri=new Uri("http://localHost:8080/test2/sss/users/1/uploadImage");
var respose=wb.UploadValues(myUri,"Post",Data)
}

the Objective-C code I am trying to use:

NSData* data = UIImageJPEGRepresentation(theImageView.image, 1.0f);
    [Base64 initialize];
    NSString *strEncoded = [Base64 encode:data];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];
    [request setURL:[NSURL URLWithString:@"http://localHost:8080/test2/sss/users/1/uploadImage"]];
   NSData* image=   [strEncoded dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES ];
    NSLog(@"%@",strEncoded);
    [request setHTTPBody:image];
     (void)[NSURLConnection connectionWithRequest:request delegate:self];

sorry again but the problem is that I don't Know if I am matching the parameters right

Ammar
  • 169
  • 1
  • 5
  • 15
  • You can always use a nice third party library like AFNetworking: http://stackoverflow.com/questions/19261481/ios-image-upload-via-afnetworking-2-0 – joao Dec 03 '13 at 10:08
  • 2
    I personally prefer implementing own stuff, 'cause in most cases you need maybe 10% of the provided code. `AFNetworking` is here, though :) https://github.com/AFNetworking/AFNetworking – Julian F. Weinert Dec 03 '13 at 10:14
  • Is there a reply from the server? – Rudiger Dec 03 '13 at 10:37
  • yes there is a reply and I can tell exactly where the problem: the server expect to find a base64 string included in the data of the url but in my case he couldn't find that string which is included in the NSData "image" in the C# case the server find it in the NameValueCollection "Data" I hope I cleared the problem enough – Ammar Dec 03 '13 at 10:42
  • @Rudiger did I make the problem more clear??? – Ammar Dec 03 '13 at 11:29
  • it won't fix your problem but [Base64 initialize]; is called automatically, you should remove it. – hooleyhoop Dec 03 '13 at 14:59

2 Answers2

1

You don't have to base64 encode the date, but use the right content type for your request. I didn't understand your problem, but here is my working method:

- (NSURLConnection *)connectionByFormUploadingData:(NSData *)data toURL:(NSURL *)url withFileName:(NSString *)fileName forFieldName:(NSString *)fieldName delegate:(id)delegate {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [[NSProcessInfo processInfo] globallyUniqueString];
    [request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

    NSMutableData *postData = [[NSMutableData alloc] initWithData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", fieldName, fileName ?: @"empty_file_name"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:data];
    [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:postData];

    return [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO];
}

EDIT

Assumed you use PHP, you save the data like this:

move_uploaded_file($_FILES['fieldName']['tmp_name'], $upload_dir.'/'.$_FILES['fieldName']['name']);

EDIT II

The last boundary in the HTTP body must be terminated using two hyphens as described in RFC1341, thanks to CouchDeveloper for this improvement!

Community
  • 1
  • 1
Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
  • unfortunately I am not using PHP, the server was built using java and the web service is restful, i don't exactly know the mechanism of saving data in the server side but I put the C# code to clear the problem as possible – Ammar Dec 03 '13 at 10:16
  • 1
    Okay, but your problem was in Obj-C, so I think the server side would be another question. The code above is extracted from a class I wrote and I used it to build a REST client class based on it. So this code will be perfect for usage with REST APIs – Julian F. Weinert Dec 03 '13 at 10:18
  • thank you very much , I know that the question is not clear enough, but I tried to close it as I can, I hope your code could solve my problem I will give it a try. – Ammar Dec 03 '13 at 10:22
  • Yea, give it a try and keep us up to date. Actually, if the code does not work, you have an other problem, likely server side. – Julian F. Weinert Dec 03 '13 at 10:24
  • I think so , I give it a try but the same error happened in the server side, sorry again for talking about the C# but Compelled cause I said It worked so I am wondering if I can make a comparative with the C# code so I can achieve the same functionality – Ammar Dec 03 '13 at 10:35
  • Compare the code again carefully. In C# you set the data to the POST field "image", in Obj-C you just set the data to the body. You have to assign it to the key. Can you edit the Java server? – Julian F. Weinert Dec 03 '13 at 10:44
  • ok, if you please @julian is there a way to make the C# code as an objective-C code or convert it, I am really sorry but the time is critical for me and I need solution so I can move on – Ammar Dec 03 '13 at 10:51
  • @Julian The terminating delimiter must be `CRLF----`. Notice the dashes at the end. – CouchDeveloper Dec 03 '13 at 12:00
  • @CouchDeveloper of course you'r right! Thanks for the improvement! – Julian F. Weinert Dec 03 '13 at 14:20
  • And it doesn't have to be followed by `CRLF`, am I right? I didn't find that info in RFC... – Julian F. Weinert Dec 03 '13 at 14:35
0

Try encoding your data a bit differently:

[strEncoded dataUsingEncoding:NSUTF8StringEncoding]

Rudiger
  • 6,749
  • 13
  • 51
  • 102