Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:57:31+00:00 2026-05-30T06:57:31+00:00

I’ve been working on getting an image to upload from my iOS app to

  • 0

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
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:57:32+00:00Added an answer on May 30, 2026 at 6:57 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.