objective-c - 将图像从iOS上传到Grails

标签 objective-c grails ios5

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

该请求正在由 Controller 处理,但是当我尝试访问请求中的文件(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
}

}

最佳答案

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

关于objective-c - 将图像从iOS上传到Grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9419744/

相关文章:

postgresql - Grails-重复键错误

video - 在我的应用程序中访问 iOS 设备的视频应用程序中的视频

objective-c - 每次加载应用程序时检查一些内容

ios - 从 UITouch 对象调用 UIGestureRecognizer

ios - 用于在页面上显示的 UIPageViewControlerDataSource 和 UiWebView

hibernate - chalice /GORM : Manually assigning instance of one object to an instance of another fails on save

maven-2 - 带有 'mvn clean install' 的 Maven Grails 构建不起作用

iOS5 打破了 UISplitViewController 旋转回调,如何手动调用?

ios - 如何启用隐式 UIView 动画?

objective-c - 如何实现socket超时?