我有这个小项目 我有一些食谱想与我的iPhone分享。 数据库具有此字段 字符串ID 字串名称 字符串配方_文本 字节[]图片
所以当我在iPhone上更改它时,我会调用此功能
+(void) sendRecipe:(BFRecipe *) recipe
{
NSString * imageStringData = @"";
if(recipe.recipeImage != nil)
imageStringData = [UIImagePNGRepresentation(recipe.recipeImage) base64Encoding];
NSDictionary * jsonRecord = [NSDictionary dictionaryWithObjectsAndKeys:recipe.recipeId,@"RecipeId",recipe.name,@"Name",recipe.recipeText,@"RecipeText",imageStringData,@"Item",nil];
NSString *urlString = [NSString stringWithFormat:@"%@/%@",[[NSUserDefaults standardUserDefaults ] objectForKey:@"Host"],@"UpdateRecipeItem"];
NSMutableURLRequest *request = [NSMutableURLRequest new];
request.HTTPMethod = @"POST";
NSData *jsonBodyData = [NSJSONSerialization dataWithJSONObject:jsonRecord options:kNilOptions error:nil];
[request setURL:[NSURL URLWithString:urlString]];
[request setvalue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setvalue:@"application/json" forHTTPHeaderField:@"accept"];
[request setHTTPBody:jsonBodyData];
NSURLSessionconfiguration *config = [NSURLSessionconfiguration defaultSessionconfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data,NSURLResponse * _Nullable response,NSError * _Nullable error) {
NSHTTPURLResponse *asHTTPResponse = (NSHTTPURLResponse *) response;
// set a breakpoint on the last NSLog and investigate the response in the debugger
// if you get data,you can inspect that,too. If it's JSON,do one of these:
}];
[task resume];
}
我的MVC服务器具有这样的功能,但从未被称为“为什么”?
public void UpdateRecipeImage(string id)
{
recipe_item item = Model.recipe_item.FirstOrDefault(ri => ri.recipe_id == new Guid(id));
....
.....
....
}