将图像从 iOS 上传到 Grails
我一直在努力将图像从我的 iOS 应用程序上传到我的 Grails 后端,但没有成功。在与另一位开发人员(间接)交谈后,建议我将内容类型从多部分/表单数据更改为多部分/二进制。 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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Obj-C 代码生成的请求似乎存在一些结构问题。以下是一些指示和代码示例: http://lists .apple.com/archives/web-dev/2007/Dec/msg00017.html
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