使用HTTPClient使用RestKit 0.20上传文件(File uploads with RestKit 0.20 using HTTPClient)
我目前正在使用以下代码发布到我的服务器:
[[[RKObjectManager sharedManager] HTTPClient] postPath:[NSString stringWithFormat:@"%@%@", baseURL, @"/api/v2/track-it/rack/individual"] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { // handle success if([[responseObject[@"result"] lowercaseString] isEqualToString:@"success"]){ // Entry was added UIViewController *otherVC = [[UIStoryboard storyboardWithName:@"App" bundle:nil] instantiateViewControllerWithIdentifier:@"Dashboard"]; [self presentViewController:otherVC animated:YES completion:nil]; [self alertWithTitle:@"Track It Entry" message:@"The entry was uploaded successfully"]; } else { // Couldn't add entry [self alertWithTitle:@"Track It Entry" message:@"An error occured. Saving entry to upload later."]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // response code is in operation.response.statusCode NSLog(@"ERROR"); }];
params
是值的NSDictionary
。如何上传文件?
我在任何没有使用托管对象的地方都找不到任何示例。I am currently using the following code to post to my server:
[[[RKObjectManager sharedManager] HTTPClient] postPath:[NSString stringWithFormat:@"%@%@", baseURL, @"/api/v2/track-it/rack/individual"] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { // handle success if([[responseObject[@"result"] lowercaseString] isEqualToString:@"success"]){ // Entry was added UIViewController *otherVC = [[UIStoryboard storyboardWithName:@"App" bundle:nil] instantiateViewControllerWithIdentifier:@"Dashboard"]; [self presentViewController:otherVC animated:YES completion:nil]; [self alertWithTitle:@"Track It Entry" message:@"The entry was uploaded successfully"]; } else { // Couldn't add entry [self alertWithTitle:@"Track It Entry" message:@"An error occured. Saving entry to upload later."]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // response code is in operation.response.statusCode NSLog(@"ERROR"); }];
params
is anNSDictionary
of values.How can I upload files with this?
I can't find any examples anywhere that aren't using a Managed Object.
原文:https://stackoverflow.com/questions/31865535
满意答案
您需要自定义模型联编程序才能正常工作。 这是您可以开始使用的简化版本:
public class CsvIntModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var key = bindingContext.ModelName; var valueProviderResult = bindingContext.ValueProvider.GetValue(key); if (valueProviderResult == null) { return false; } var attemptedValue = valueProviderResult.AttemptedValue; if (attemptedValue != null) { var list = attemptedValue.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries). Select(v => int.Parse(v.Trim())).ToList(); bindingContext.Model = list; } else { bindingContext.Model = new List<int>(); } return true; } }
并以这种方式使用它(从路由中删除
{ids}
):[HttpGet] [Route("api/NewHotelData")] public HttpResponseMessage Get([ModelBinder(typeof(CsvIntModelBinder))] List<int> ids)
如果您想保留
{ids}
的路线,您应该将客户端请求更改为:api/NewHotelData/1,2,3,4
另一个选项( 没有自定义模型绑定)正在将获取请求更改为:
?ids=1&ids=2&ids=3
You'll need custom model binder to get this working. Here's simplified version you can start work with:
public class CsvIntModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var key = bindingContext.ModelName; var valueProviderResult = bindingContext.ValueProvider.GetValue(key); if (valueProviderResult == null) { return false; } var attemptedValue = valueProviderResult.AttemptedValue; if (attemptedValue != null) { var list = attemptedValue.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries). Select(v => int.Parse(v.Trim())).ToList(); bindingContext.Model = list; } else { bindingContext.Model = new List<int>(); } return true; } }
And use it this way (remove
{ids}
from route):[HttpGet] [Route("api/NewHotelData")] public HttpResponseMessage Get([ModelBinder(typeof(CsvIntModelBinder))] List<int> ids)
If you want to keep
{ids}
in route, you should change client request to:api/NewHotelData/1,2,3,4
Another option (without custom model binder) is changing get request to:
?ids=1&ids=2&ids=3
相关问答
更多从Java Servlet向Web API发送get请求(To send get request to Web API from Java Servlet)
Web API 2不处理整数的PATCH请求(Web API 2 does not process PATCH requests for Integers)
从客户端调用Web API(Calling Web API from client)
发送多个Web请求(Send multiple Web requests)
发送对象列表到Web API(Sending list of objects to Web API)
如何在正文请求中将参数发送到Web API?(How to send parameters to web API in the body request?)
如何向Web api发送整数列表2获取请求?(How to send a list of integers to web api 2 get request?)
PutAsync不向web api发送请求,但fiddler工作正常(PutAsync doesn't send request to web api, but fiddler works fine)
将整数数组发布到asp.net web api(Posting array of integers to asp.net web api)
如何通过Angular向ASP.net web API发送帖子请求(How to send post request through Angular to ASP.net web API)
相关文章
更多httpclient get请求
HttpClient 上传文件
HttpClient CacheConfig缓存处理示例
HttpClient DELETE请求示例
Httpclient整合Spring教程
HttpClient 重定向处理
HttpClient PUT请求示例
httpclient依懒包官网下载及httpclient maven依懒包获取
HttpClient 请求添加Header头部信息
Hadoop0.20+ custom MultipleOutputFormat
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!