Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodeAttachFile example for Services 7.x-3.x #46

Closed
c5f opened this issue Jul 2, 2012 · 8 comments
Closed

nodeAttachFile example for Services 7.x-3.x #46

c5f opened this issue Jul 2, 2012 · 8 comments

Comments

@c5f
Copy link

c5f commented Jul 2, 2012

Hi Kyle,

It looks like file_attach is still a work in progress on Services 7.x-3.x: http://drupal.org/node/1484992

I'm wondering if there's a DIOS 2.0 example out with working file attachment.

I am unable to attach files from DIOS at the moment, and I don't know if my issue is my Drupal configuration or DIOS.

@JC2011
Copy link

JC2011 commented Jul 13, 2012

I'm trying to attach file to a node and it doesn't work.
The data was sent and i received an error :
Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0x73290a0 {NSErrorFailingURLKey=http://mysite/api/node/84/attach_file?field_name=field_photo, NSLocalizedDescription=Expected status code in (200-299), got 404}

Is the nodeAttachFile method work ?

I'm working on 7.14 drupal version .

Thanks in advance

@kylebrowning
Copy link
Owner

Does this code help you?

  [[AppDelegate customStatusBar] showWithStatusMessage:@"Uploading"];
  NSData *imgData = UIImageJPEGRepresentation(img, 1.0);

  NSMutableDictionary *file = [[NSMutableDictionary alloc] init];
  NSString *base64Image = [(NSData *)imgData base64EncodedString];
  [file setObject:base64Image forKey:@"file"];
    NSString *timestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]];
  NSString *imageTitle = [[articleTitle text] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
  NSString *filePath = [NSString stringWithFormat:@"%@%@.jpg",@"public://", imageTitle];
  NSString *fileName = [NSString stringWithFormat:@"%@.jpg", imageTitle];
  [file setObject:filePath forKey:@"filepath"];
  [file setObject:fileName forKey:@"filename"];
  [file setObject:timestamp forKey:@"timestamp"];
  NSString *fileSize = [NSString stringWithFormat:@"%d", [imgData length]];
  [file setObject:fileSize forKey:@"filesize"];
  [DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"];
    [file removeObjectForKey:@"file"];
    [self setFileData:file];
    [[AppDelegate customStatusBar] hide];
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    #warning We should let the user know rather than dumping a log
    NSLog(@"Something went wrong: %@", [error localizedDescription]);
  }];
  [self dismissModalViewControllerAnimated:YES];

@JC2011
Copy link

JC2011 commented Jul 19, 2012

Thanks but how to link the file with a node on site ???

Best regards,

Envoyé de mon iPhone

Le 18 juil. 2012 à 23:12, "Kyle Browning" reply@reply.github.com a écrit :

Does this code help you?

 [[AppDelegate customStatusBar] showWithStatusMessage:@"Uploading"];
 NSData *imgData = UIImageJPEGRepresentation(img, 1.0);

 NSMutableDictionary *file = [[NSMutableDictionary alloc] init];
 NSString *base64Image = [(NSData *)imgData base64EncodedString];
 [file setObject:base64Image forKey:@"file"];
   NSString *timestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]];
 NSString *imageTitle = [[articleTitle text] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
 NSString *filePath = [NSString stringWithFormat:@"%@%@.jpg",@"public://", imageTitle];
 NSString *fileName = [NSString stringWithFormat:@"%@.jpg", imageTitle];
 [file setObject:filePath forKey:@"filepath"];
 [file setObject:fileName forKey:@"filename"];
 [file setObject:timestamp forKey:@"timestamp"];
 NSString *fileSize = [NSString stringWithFormat:@"%d", [imgData length]];
 [file setObject:fileSize forKey:@"filesize"];
 [DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) {
   [file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"];
   [file removeObjectForKey:@"file"];
   [self setFileData:file];
   [[AppDelegate customStatusBar] hide];
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   #warning We should let the user know rather than dumping a log
   NSLog(@"Something went wrong: %@", [error localizedDescription]);
 }];
 [self dismissModalViewControllerAnimated:YES];

Reply to this email directly or view it on GitHub:
#46 (comment)

@JC2011
Copy link

JC2011 commented Jul 27, 2012

I found the solution.
After saving file, I set my field_photo of my node with the fid.

@loh-xx
Copy link

loh-xx commented Sep 3, 2012

Hi JC2011,
Can you tell me how you set node field_photo with the fid.
Thank

@loh-xx
Copy link

loh-xx commented Sep 5, 2012

Thank you, I did it.

@karschsp
Copy link

@loh Care to explain how?

@karschsp
Copy link

Well, I eventually figured it out. I'm sure it's wrong, because I don't really know what I'm doing, but it works. Anyway, after uploading the file, grab the fid and start creating a new node like so...

NSMutableDictionary *newNodeData = [NSMutableDictionary new];
[newNodeData setValue:fldTitle.text forKey:@"title"];
[newNodeData setValue:@"photo" forKey:@"type"];
[newNodeData setValue:@"und" forKey:@"language"];

These next four lines are where you add the file (in my case, the field is field_photo)...

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSString stringWithFormat:@"%@", fid] forKey:@"fid"];
NSDictionary *fidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:dict] forKey:@"und"];
[newNodeData setObject:fidLangDict forKey:@"field_photo"];

Then finish up...

[DIOSNode nodeSave:newNodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //Successful node Creation, etc, etc

Suggestions welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants