I’ve been working on getting an image to upload from my iOS app to my Grails backend without any success. After talking (indirectly) to another developer it was suggested that I change my content type from multipart/form-data to multipart/binary. The Objective-C code was written after looking at numerous examples.
The request is being handled by the controller but when I attempt to access the file in the request (request.fileName(‘imageToAttach’)) I get a null value.
Here are the three parts of my app (backend and client side) in question. Anyone see what I might be doing wrong?
+ (BOOL)uploadImage:(UIImage *)image withName:(NSString *)fileName toURL:(NSURL *)url {
// url points to /my/uploadImage which is the uploadImage action in MyController
NSData *imageData = UIImageJPEGRepresentation(image, 100);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0xOhHaiICanHazB0undary";
NSString *contentType = [NSString stringWithFormat:@"multipart/binary; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\n--%@--\n",boundary] dataUsingEncoding: NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: binary; name=\"imageToAttach\"; filename=\"%@\"\n",fileName]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\n\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\n--%@--\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
return YES;
}
MyController.groovy:
def attachImageToOi = {
println "Uploading image..."
def result
def fileNames = []
if(request.method == 'POST') {
def imageFile = request.getFile('imageToAttach')
println imageFile?.inputStream?.text
if (imageFile && !imageFile.isEmpty()){
def imagePath = fileUploadService.uploadFile(imageFile, params.imageFileName, "/userFiles")
if (imagePath != null) {
fileNames << imagePath
}
} else {
println "Looks like the image file is empty or null..."
}
} else {
render "This action only accepts POST"
return
}
result = [status:200, data:[fileNames:fileNames]]
render result as JSON
return
}
FileUploadService.groovy:
def uploadFile(MultipartFile file, String name, String destinationDirectory) {
def servletContext = ServletContextHolder.servletContext
def storagePath = servletContext.getRealPath(destinationDirectory)
// create storage path directory if it does not exist
def storagePathDirectory = new File(storagePath)
if (!storagePathDirectory.exist()) {
println "Creating directory: ${storagePath}"
if (storagePathDirectory.mkdirs()){
println "success"
} else {
println "failed"
}
}
// store the file
if (!file.isEmpty()) {
def fullPathToFile = "${storagePath}/${name}"
file.transferTo(new File(fullPathToFile))
println "Saved file: ${fullPathToFile}"
return fullPathToFile
} else {
println "File ${file.inspect()} was empty!"
return null
}
}
Looks like there are a few structural issues with the request generated by your Obj-C code. Here are a few pointers and a code sample: http://lists.apple.com/archives/web-dev/2007/Dec/msg00017.html