2012-02-23 99 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