我一直致力于将图片从我的iOS应用程序上传到我的Grails后端,但没有任何成功。在(间接)与另一位开发人员交谈之后,建议我将我的内容类型从multipart / form-data更改为multipart / binary。 Objective-C代码是在查看了大量示例之后编写的。
请求是由控制器处理,但是当我尝试访问请求中的文件时(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 :(得分:0)
看起来Obj-C代码生成的请求存在一些结构性问题。以下是一些指针和代码示例:http://lists.apple.com/archives/web-dev/2007/Dec/msg00017.html