Drupal REST服务器,iOS App使用ASIHTTPRequest的多部分/表单数据图像上载问题

时间:2012-03-27 04:00:35

标签: iphone drupal rest asihttprequest

我正在开发一个iOS应用程序,它将图像和文本上传到Drupal REST服务器。图像和文本将存储在MySQL表中。

我正在使用ASIHTTPRequest对我的REST服务器执行POST。 在服务器端,我按照此示例设置了Drupal REST服务器:http://drupal.org/node/1246470

我遇到的问题是我不知道如何在REST服务器上引用该图像。每当我尝试发送信息时,我都会收到错误“HTTP / 1.0 406 Not Acceptable:Missing issue attribute image”。任何人都可以指出我做错了什么或我错过了什么?

我的iOS版本是5.1。 我的Drupal版本是7.8。

我的iOS代码如下:

 NSString* timestamp = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];

//Add the timestamp to the filename
NSMutableString* filename = [NSMutableString stringWithString:@"issue-"];

[filename appendString: timestamp];
[filename appendString: @".png"];

//Convert the image into an NSData object
NSData* imageData = UIImagePNGRepresentation(imageView.image);

NSURL *url = [NSURL URLWithString: SERVICE_URL];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];

[request setPostValue: emailAddress forKey:@"email"];
[request setPostValue: details forKey:@"details"];
[request setData: imageData withFileName: filename andContentType: @"image/png" forKey: @"image"];

[request setPostFormat:ASIMultipartFormDataPostFormat];

[request setRequestMethod:@"POST"];

[request startSynchronous];

至于服务器端,这里是我设置hook_services_resources的地方:

/**
* Implementation of hook_services_services().
*/
function issue_services_resources() {
  return array(
    'issue' => array(
      'retrieve' => array(
        'help' => 'Retrieves an issue',
        'file' => array('file' => 'inc', 'module' => 'issue'),
        'callback' => '_issue_retrieve',
        'access callback' => '_issue_access',
        'access arguments' => array('view'),
        'access arguments append' => TRUE,
        'args' => array(
          array(
            'name' => 'id',
            'type' => 'int',
            'description' => 'The id of the issue to get',
            'source' => array('path' => '0'),
            'optional' => FALSE,
          ),
        ),
      ),
      'create' => array(
        'help' => 'Creates an issue',
        'file' => array('file' => 'inc', 'module' => 'issue'),
        'callback' => '_issue_create',
        'access arguments' => array('issue service create'),
        'access arguments append' => FALSE,
        'args' => array(
          array(
            'name' => 'data',
            'type' => 'struct',
            'description' => 'The issue object',
            'source' => 'data',
            'optional' => FALSE,
          ),
        ),
      ),
      'update' => array(
        'help' => 'Updates an issue',
        'file' => array('file' => 'inc', 'module' => 'issue'),
        'callback' => '_issue_update',
        'access callback' => '_issue_access',
        'access arguments' => array('update'),
        'access arguments append' => TRUE,
        'args' => array(
          array(
            'name' => 'id',
            'type' => 'int',
            'description' => 'The id of the node to update',
            'source' => array('path' => '0'),
            'optional' => FALSE,
          ),
          array(
            'name' => 'data',
            'type' => 'struct',
            'description' => 'The issue data object',
            'source' => 'data',
            'optional' => FALSE,
          ),
        ),
      ),
      'delete' => array(
        'help' => 'Deletes an issue',
        'file' => array('file' => 'inc', 'module' => 'issue'),
        'callback' => '_issue_delete',
        'access callback' => '_issue_access',
        'access arguments' => array('delete'),
        'access arguments append' => TRUE,
        'args' => array(
          array(
            'name' => 'nid',
            'type' => 'int',
            'description' => 'The id of the issue to delete',
            'source' => array('path' => '0'),
            'optional' => FALSE,
          ),
        ),
      ),
      'index' => array(
        'help' => 'Retrieves a listing of issues',
        'file' => array('file' => 'inc', 'module' => 'issue'),
        'callback' => '_issue_index',
        'access callback' => 'user_access',
        'access arguments' => array('access content'),
        'access arguments append' => FALSE,
        'args' => array(
          array(
            'name' => 'page',
            'type' => 'int',
            'description' => '',
            'source' => array(
              'params' => 'page',
            ),
            'optional' => TRUE,
            'default value' => 0,
          ),
          array(
           'name' => 'parameters',
           'type' => 'array',
           'description' => '',
           'source' => 'param',
           'optional' => TRUE,
           'default value' => array(),
          ),
        ),
      ),
    ),
  );
}

我有预感,它可能是“创造”的论据。从我拥有的,我只有“数据”或类型“结构”。看起来我可能需要为图像添加一个额外的参数?如果是这样,会是什么?

这里是我检查和设置406错误的地方:

function _issue_create($data) {

if (isset($data) &&  is_array($data)) $data=(object) $data; 

  unset($data->id);
  $data->created = time();
  $data->modified = time();

  if (!isset($data->email)) {
    return services_error('Missing issue attribute email address', 406);
  }

  if (!isset($data->details)) {
    return services_error('Missing issue attribute details', 406);
  }

  if (!isset($data->image)) {
    return services_error('Missing issue attribute image', 406);
  }

  issue_write_issue($data);

  return (object)array(
    'id' => $data->id,
    'uri' => services_resource_uri(array('issue', $data->id)),
  );
}

问题出在我检查的地方:

if (!isset($data->image))

这似乎不是检查图像数据是否存在的正确方式......参考图像数据的正确方法是什么?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

最后知道我要做什么。傻傻的我,我忘了有一个名为$ _FILES的php全局,这是上传的文件!当我记得这一点时,很容易将图像存储到MySQL表中。