Malarkey86
Malarkey86

Reputation: 107

I get corrupt file when upload image from Objective-c to server

Hello this is what I do form objective-c and xcode 9. The code on server works because I can upload good file image from Android. I tried doing some changes, but I get the same error on server.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my-url"]];

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

NSString *connection = @"Keep-Alive";
[request addValue:connection forHTTPHeaderField:@"Connection"];

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

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"%@\"\r\n",newName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"%@", [request allHTTPHeaderFields]);

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
    if(data.length > 0) {
        //success
        NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Response %@",responseString);
        NSError *error;
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        if(error == nil){
            if([[responseDictionary objectForKey:@"ok"]boolValue]) {
                NSString *fileURL = [[responseDictionary objectForKey:@"file"] objectForKey:@"url_download"];
                NSLog(@"File URL %@", fileURL);
            } else {
                NSLog(@"Could Not upload file ");
            }
        }
    }
}] resume];

The code below contains methods from server where the image should be decoded. That code works fine when i send the same multipart form from Android code.

@POST
@Path("/uploadFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {

    String output = "-1";
    if (contentDispositionHeader.getFileName().split("~@~").length > 2) {

        String codMun = contentDispositionHeader.getFileName().split("~@~")[0];
        String fileName = contentDispositionHeader.getFileName().split("~@~")[1] + "~@~"
                + contentDispositionHeader.getFileName().split("~@~")[2];
        String filePath = SERVER_UPLOAD_LOCATION_FOLDER + "/resources/" + fileName;

        if (fileInputStream != null) { 
            if (saveFile(fileInputStream, filePath) < 0) {
                Response.status(200).entity("{\"error\":\"-2\"}").build();
            }
            output = "{\"error\":\"0\"}";

        } else {
            return Response.status(200).entity("{\"error\":\"-1\"}").build();
        }
    }

    return Response.status(200).entity(output).build();
}


private int saveFile(InputStream uploadedInputStream, String serverLocation) {

    int flagExit = -1;
    try {

        OutputStream outpuStream = new FileOutputStream(new File(serverLocation));

        if (outpuStream != null) {

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = uploadedInputStream.read(bytes)) != -1) {
                outpuStream.write(bytes, 0, read);
            }
            outpuStream.flush();
            outpuStream.close();
            flagExit = 1;
        }
    } catch (IOException e) {
        logger.error("Error Output : ", e);
        flagExit = -3;
        e.printStackTrace();
    }
    return flagExit;

}

Upvotes: 0

Views: 414

Answers (1)

battlmonstr
battlmonstr

Reputation: 6300

Looking at the code, I have simplified it a bit (removed irrelevant lines and rearranged), I was able to spot some mistakes:

  • At the start of the body you have an empty line, you don't need it (it is automatically inserted by the library).
  • You have "--" before or after boundary stringWithFormat, you don't need it. You can put "--" inside the boundary if you want to, but you don't have to. It is important that all boundaries match.
  • You are missing an empty line before imageData inside body. Your data starts on the next line after "Content-Disposition", but there needs to be an empty line in between (just "\r\n").

Minor notes:

  • You should put a 2nd content type header for your image. Since you have a JPEG it should be "Content-Type: image/jpeg\r\n" after "Content-Disposition: ...".
  • This is redundant: [body appendData:[NSData dataWithData:imageData]];, because [body appendData:imageData]; works as well.

Check this answer: https://stackoverflow.com/a/23517227/1009546

It is a good example of how it should look like, and you can debug with "nc -l localhost 8000" command if you iOS simulator sends correct stuff out if you set your URL to "http://localhost:8000".

Upvotes: 1

Related Questions