0

I am developing an iOS application using .NET web service. In one of the web services, I have to send image as HttpPostedFileBase in a parameter called "file", which will return the image URL and status parameters as response. Actually I am not sure what is HttpPostedFileBase. But I am trying to send that as bytes. I am getting image URL as null from WS. I guess the WS call works fine, but the problem is in the way I send the image. I am using the following code to append image data to HTTP body.

NSData *imageData = UIImageJPEGRepresentation(image, 1);
[postData appendData:[[NSString stringWithFormat:@"--%@\r--\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"file.jpeg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[request addValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%i", [postData length]] forHTTPHeaderField:@"Content-Length"];
[postData appendData:[[NSString stringWithFormat:@"--%@\r--\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

Please let me know how can I get this working. Let me know if you need anymore detail.

Thanks.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • You are sending your content without content header part to the server, that's why it's not working. Change this line "`mutablePostData = [NSMutableData dataWithData:imageData];`" to "`[mutablePostData appendData:imageData];`". And, also, I don't see any content boundaries and endlines?? – Fahri Azimov Aug 26 '13 at 11:16
  • @FahriAzimov, Sorry. My mistake. Please check the edited code now. Thanks. – EmptyStack Aug 26 '13 at 11:24
  • Quick search on SO gave this result: http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post Good Luck! – Fahri Azimov Aug 26 '13 at 11:26
  • @FahriAzimov Thanks. The problem was with the headers. I thought there is going to be a different format for .NET WS. Thanks! – EmptyStack Aug 27 '13 at 06:13
  • @EmptyStack hai sir how r u – Venk Dec 03 '13 at 10:23

2 Answers2

0

Sorry guys. The problem was in the HTTP headers. The link (ios Upload Image and Text using HTTP POST) posted by @FahriAzimov above helped me. My bad, actually I was confused that there is going to be a different format for .NET web services.

I had to send the image as we do for normal web services. The working code is below:

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

[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

Thanks a lot for everyone's help.

Community
  • 1
  • 1
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
-1

use ASIHTTPRequest libraray, its very good, you can send the image as attachment to your .net server.... http://allseeing-i.com/ASIHTTPRequest/How-to-use

here is sample code

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

// Upload a file on disk
[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
forKey:@"photo"];

// Upload an NSData instance
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
Pradeep
  • 1,043
  • 7
  • 12
  • 2
    Please stop using ASIHTTP. Developer has removed support from it and is not ARC enabled too. – Vaibhav Gautam Aug 26 '13 at 11:13
  • Thanks! The first line on the page in the above link says that ***"Please note that I am no longer working on this library - you may want to consider using something else for new projects. :)"***. Do you think its still safe to use that? – EmptyStack Aug 26 '13 at 11:14
  • 1
    @EmptyStack: i am using ASIHTTPRequest in my prjs and its live in Apple, i dnt think there is a problem. – Pradeep Aug 26 '13 at 11:30