-1

I want to upload one image to server using http post method. the requirement is like in the body data should be attached as byte stream.

I converted image to NSData and attached that data to body of NSUrlrequest. but i am getting 404 in status code of response.

NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:UIImageData]];

[request setHTTPBody:body]; 

but i am getting 404 error in status code.

is byte stream is same as NSData ?

if not then how to send byte stream data to server using NSURLConnection ?

Thanks in advance.

Saad Chaudhry
  • 1,392
  • 23
  • 37
uttam
  • 1,364
  • 4
  • 20
  • 35
  • Show a lot more of the code please – matt Apr 15 '14 at 17:37
  • Your error is more than likely due to your URL being incorrect, but you should be Base64 encoding your image as the body. – Erik Apr 15 '14 at 17:45
  • Please check your URL and refer to this answer to check your code against it: http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post – neowinston Apr 15 '14 at 17:46
  • please add more code and go through this link http://stackoverflow.com/questions/20869848/unable-to-upload-image-to-server-ios – Saad Chaudhry Apr 15 '14 at 18:50

2 Answers2

0

What about the URL you're using to send the image to?

A 404 error code translates to Not Found (http://en.wikipedia.org/wiki/HTTP_404), so the first step should be making sure yore using a valid URL.

Aloha Silver
  • 1,394
  • 17
  • 35
0

Try to make the upload that way:

NSData *yourData = //load your data here;

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:UPLOAD_URL]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", UPLOAD_FILE, UPLOAD_FILE] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:yourData]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    [connection start];
Lucas Brito
  • 1,028
  • 1
  • 18
  • 36