I have a problem uploading image to the server. I can use the code below from the iPhone simulator to send to our server successfully. When I used this code below on real device like iPad 2, the image on the server seems to be corrupted. The data was extracted from main source url from AlAssetsLibrary. I don't know why the value of 512 was add with the data length from the code below. Does the layout of the message header important e.g. content-disposition have to come before the content-type?
NSDateFormatter *date_formatter=[[NSDateFormatter alloc]init];
[date_formatter setDateFormat:@"YYYY-MM-DD-hh-mm-ss"];
NSString *sDateTime = [date_formatter stringFromDate:[NSDate date]];
[date_formatter release];
// from http://www.cocoadev.com/index.pl?HTTPFileUpload
NSMutableURLRequest *urlRequest =
[NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:
[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundry]
forHTTPHeaderField:@"Content-Type"];
NSMutableData *postData =
[NSMutableData dataWithCapacity:[data length] + 512];
[postData appendData:
[[NSString stringWithFormat:@"--%@\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:
[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"%@\" filename=\"%@_%@.jpg\"\r\n", FORM_FLE_INPUT,sDateTime, filename]
dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:data];
[postData appendData:
[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postData];
return urlRequest;
Did I missing anything in the code above that will cause the problem?
This is the NSData I generated from AlAsset defaultRepresentation.
int byteArraySize = asset.defaultRepresentation.size;
NSMutableData* data = [[NSMutableData alloc]initWithCapacity:byteArraySize];
void* bufferPointer = [data mutableBytes];
[data release];
NSError* error=nil;
[asset.defaultRepresentation getBytes:bufferPointer fromOffset:0 length:byteArraySize error:&error];
if (error) {
NSLog(@"%@",error);
}
data = [NSMutableData dataWithBytes:bufferPointer length:byteArraySize];
Now I can send gif but not other format