使用 RESTKit 上传 jpeg 文件时 Tomcat 上的 HTTP/1.1 200 213
全部, 我正在使用 RestKit for iOS 将 JPEG 文件上传到我的 Java Web 服务。我参考了这个
教程进行开发文件上传网络服务,当我通过网络浏览器使用它时,它工作得非常好。
但是,当我尝试使用 RESTKit 上传文件时,在 TOMCAT 日志中我收到 HTTP/1.1 200 213 状态代码,但我的文件未上传。
这是我的 RESTKit 代码:
RKObjectManager* manager = [RKObjectManager sharedManager];
RKObjectLoader* objectLoader = [manager objectLoaderWithResourcePath:@"/fileuploaded" delegate:self];
objectLoader.method = RKRequestMethodPOST;
UIImage *image = [UIImage imageNamed:@"rental_car_bill.jpeg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
// attach image
RKParams *params = [RKParams paramsWithDictionary:(NSDictionary*)objectLoader.params];
RKParamsAttachment *attachment = [params setData:imageData
MIMEType:@"image/jpeg" forParam:@"photo"];
attachment.fileName = @"samplejpeg";
objectLoader.params = params;
objectLoader.targetObject = self;
[objectLoader send];
编辑: 上面的实现确实有效并且文件确实被上传。但是,在委托方法中: - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
超出此范围后,我的应用程序在 [RKObjectLoader dealloc]; 处崩溃;
All,
I am using RestKit for iOS to upload a JPEG file to my Java web service. I referred to this
tutorial for developing file upload web service and it works perfectly fine when i use it through my web browser.
However, when i try to upload a file using RESTKit then in TOMCAT logs i get HTTP/1.1 200 213 status code and my file is not uploaded.
Here is my RESTKit code:
RKObjectManager* manager = [RKObjectManager sharedManager];
RKObjectLoader* objectLoader = [manager objectLoaderWithResourcePath:@"/fileuploaded" delegate:self];
objectLoader.method = RKRequestMethodPOST;
UIImage *image = [UIImage imageNamed:@"rental_car_bill.jpeg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
// attach image
RKParams *params = [RKParams paramsWithDictionary:(NSDictionary*)objectLoader.params];
RKParamsAttachment *attachment = [params setData:imageData
MIMEType:@"image/jpeg" forParam:@"photo"];
attachment.fileName = @"samplejpeg";
objectLoader.params = params;
objectLoader.targetObject = self;
[objectLoader send];
EDIT:
Above implementation does work and the file does get uploaded. However, in the delegate method: - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
After it gets out of the scope of this, then my application crashes at [RKObjectLoader dealloc];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用restkit上传图片,我使用以下方法,我更喜欢使用块来避免我的应用程序出现一些问题,也许这样你就可以避免崩溃:
To upload pictures using restkit I use the following methods, I prefer using blocks to avoid some problems on my app, maybe that way you can avoid your crash: