Skip to content Skip to sidebar Skip to footer

How To Upload Image Data To Server With Specific File Name?

Xcode Source Code NSString *path = [self pathOfCharacterFolder]; path = [path stringByAppendingPathComponent:[[self fileNamesOfCharacters] objectAtIndex:sender.tag]]; NSDat

Solution 1:

Use following code :(rn is changed to : \r\n) carriage return and new line character.

NSMutableData*body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"123456700.png\"\r\n"] 
                      dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

Upadted Code:

also I am not sure about your imageData. How you create it?

NSMutableData*body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=picture; filename=\"123456700.png\"\r\n"] 
                          dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

Post a Comment for "How To Upload Image Data To Server With Specific File Name?"