質問

I am trying to upload file with like this:

   NSMutableDictionary * lParameters = [NSMutableDictionary dictionary];
   [lParameters setObject:@"temp.jpg" forKey:@"file"];

  NSMutableURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:@"uploads/add.json" parameters:lParameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
  {
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    [formData appendPartWithFileData:data name:@"temp.jpg" fileName:@"temp.jpg" mimeType:@"image/jpeg"];
  }];

  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
  ....

My upload json looking like this:

http://base/url/uploads/add.json

And have only one parameter "file". Server returns me the error: "Mandatory parameter not specified: file". How should I set the parameter. Why doesn't work:

   NSMutableDictionary * lParameters = [NSMutableDictionary dictionary];
   [lParameters setObject:@"temp.jpg" forKey:@"file"];

Help please.

役に立ちましたか?

解決

You probably shouldn't be setting temp.jpg in the parameters dictionary. Instead, try setting the name in appendPartWithFileData:... to file.

If that doesn't fix it, please provide a backtrace from the server corresponding to the error.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top