简体   繁体   English

Drupal REST服务器,使用ASIHTTPRequest从iOS App中获取多部分/表单数据图像上传问题

[英]Drupal REST server, multipart/form-data image upload issue from iOS App using ASIHTTPRequest

I am developing an iOS app that uploads an image and text to a Drupal REST server. 我正在开发一个iOS应用,该应用将图像和文本上传到Drupal REST服务器。 The image and text are to be stored in a MySQL table. 图像和文本将存储在MySQL表中。

I am using ASIHTTPRequest to to do a POST to my REST server. 我正在使用ASIHTTPRequest对我的REST服务器进行POST。 On the server side, I followed this example for setting up a Drupal REST server: http://drupal.org/node/1246470 在服务器端,我按照以下示例设置了Drupal REST服务器: http : //drupal.org/node/1246470

The issue I am having is I do not know how to to reference the image on the REST server. 我遇到的问题是我不知道如何引用REST服务器上的映像。 Whenever I try to send the information, I keep getting the error "HTTP/1.0 406 Not Acceptable: Missing issue attribute image" . 每当我尝试发送信息时,都会不断出现错误“ HTTP / 1.0 406不可接受:缺少问题属性图像” Can anyone point out what I'm doing wrong or what I am missing? 谁能指出我做错了什么或我错过了什么?

My iOS version is 5.1. 我的iOS版本是5.1。 My Drupal version is 7.8. 我的Drupal版本是7.8。

My iOS code is the following: 我的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];

As for the server side, here is where I setup the hook_services_resources: 至于服务器端,这是我设置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(),
          ),
        ),
      ),
    ),
  );
}

I have a hunch it may be the arguments for the "create". 我预感这可能是“创建”的参数。 From what I have, I only have the "data" or type "struct". 从我所拥有的,我只有“数据”或键入“结构”。 It looks I may need to have an additional argument for the image? 看起来我可能需要为图像添加其他参数? If so, what would it be? 如果是这样,那会是什么?

and here is where I do the check and setting of the 406 error: 这是我检查和设置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)),
  );
}

The issue is where I do the check: 问题是我要检查的地方:

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

It seems this is not the correct manner for checking whether the image data is present... What is the correct way to reference the image data? 看来这不是检查图像数据是否存在的正确方法。引用图像数据的正确方法是什么?

Thank you for your help in advance. 提前谢谢你的帮助。

Finally know what I have to do. 终于知道我该怎么办。 Silly me, I forgot that there was the php global called $_FILES which is the uploaded file! 愚蠢的我,我忘了有一个名为$ _FILES的php全局文件,它是上传的文件! When I remembered that, it was very easy to store the image into the MySQL table. 当我记得这一点时,将图像存储到MySQL表中非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASIHTTPRequest POST多部分/表单数据 - POST multipart/form-data with ASIHTTPRequest 使用来自iPhone应用程序的ASIHTTPRequest在服务器中上传图像文件 - Upload image file in server using ASIHTTPRequest from IPhone app 使用Alamofire和multipart / form-data - Using Alamofire and multipart/form-data iOS:从照片库中选择一个GIF,转换为NSData以用于multipart / form-data - iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data iOS:使用多部分/表单数据时出现“ 400错误请求” - IOS: getting '400 Bad Request' when using multipart/form-data 在HTTP multipart / form-data内容类型(iOS)中转义CRLF - escaping CRLF in HTTP multipart/form-data content type (iOS) 获取System.InvalidOperationException:请求格式无效:通过AFNetworking上传图像(数据)时出现multipart / form-data错误 - Getting System.InvalidOperationException: Request format is invalid: multipart/form-data error while upload image(data) via AFNetworking 服务器不接受我的multipart / form-data - Server does not accept my multipart/form-data 使用ASIHTTPRequest从iPhone将多个图像上传到服务器 - Upload more than one image onto server from iPhone using ASIHTTPRequest 使用ASIHTTPRequest从iPhone将文件上传到服务器 - Upload file onto Server from the IPhone using ASIHTTPRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM