2012-02-23 56 views
2

我一直在努力将图像从我的iOS应用上传到我的Grails后端,但没有取得任何成功。在与其他开发人员(间接)交谈之后,建议将我的内容类型从multipart/form-data更改为multipart/binary。 Objective-C代码是在查看大量示例后编写的。将图像从iOS上传到Grails

请求被控制器处理,但是当我尝试访问请求中的文件(request.fileName('imageToAttach'))时,我得到一个空值。

这是我的应用程序(后端和客户端)的三个部分有问题。任何人看到我可能做错了什么?

+ (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 
} 

}

回答

0

看起来您的Obj-C代码生成的请求存在一些结构性问题。这里有几个指针和一个代码示例:http://lists.apple.com/archives/web-dev/2007/Dec/msg00017.html

+0

谢谢!这是我需要的正确方向的微调。问题是行结尾和内容类型的组合。正确的行尾应该是'\ r \ n',而不是'\ n'。随意查看[工作代码的要点](https://gist.github.com/1895074) – 2012-02-23 21:16:32