【问题标题】:Yii framework webservice upload image in postmanYii框架webservice在postman中上传图片
【发布时间】:2017-10-30 10:57:12
【问题描述】:

我正在尝试从邮递员表单数据上传图像。我无法获得输入

 URL: localhost/sampleyii/web-app/web/imageuploads
 Method: POST
 {"Content-Type":"application/x-www-form-urlencoded"}
 Form-data
  id : 2
  image : test.jpg (type:file)   

在我的 yii 编码中:

 $model = new TblImgUpload(); 
 print_r(Yii::$app->request->post());
 $model->load(Yii::$app->request->post(),'');

邮递员的回应:

Array
 (
[------WebKitFormBoundarySHaGMA86OHQV87iT
Content-Disposition:_form-data;_name] => "id"

sdfgdfgdf
------WebKitFormBoundarySHaGMA86OHQV87iT
Content-Disposition: form-data; name="wretr"; filename="test.jpeg"
Content-Type: image/jpeg........
....................

我已经尝试过 Content-Type => 'multipart/form-data'。但我无法获取输入请求。

我希望将发布请求输入作为表单数据(文件)作为数组。

请帮助提前谢谢。

【问题讨论】:

    标签: php web-services file-upload yii2 postman


    【解决方案1】:

    $model->load() 需要具有特定名称的帖子值。所以在你的邮递员调用中,'id' 必须是 'TblImgUpload[id]' 并且 'image' 必须是 'TblImgUpload[image]'。

    请注意,'id' 和 'image' 必须在模型的 rules method 中作为 'safe' 属性返回,否则它们将不会被大量加载。

    【讨论】:

      【解决方案2】:

      使用yii\web\UploadedFile::getInstance()yii\web\UploadedFile::getInstanceByName() 检索上传的文件。在official docs 中有一个很好的例子来说明如何实现它。

      这是我曾经用于 REST 应用程序的快速 sn-p 代码:

      控制器:

      use Yii;
      use yii\web\UploadedFile;
      use app\models\UploadImageForm;
      
      class UploadController extends ActiveController
      {
      
        public function actionImage()
        {
            $model = new UploadImageForm(); 
      
            if (Yii::$app->request->isPost) {
                $model->load(Yii::$app->getRequest()->getBodyParams(), '');
                $model->imageFile = UploadedFile::getInstanceByName('imageFile'); 
                /**
                 * Note: when validation fails, returning the model itself 
                 * will output the failing rules on a '422' response status.
                **/
                return $model->upload() ?: $model;
            }
        }
      
      }
      

      型号:

      use Yii;
      use yii\helpers\Url;
      use yii\web\ServerErrorHttpException;
      
      
      class UploadImageForm extends \yii\base\Model
      {
          public $imageFile;
          public $documentPath = 'uploads/';
      
          public function rules()
          {
              return [
                  [['imageFile'], 'required'],
                  [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg',  'maxSize' => 4194304, 'tooBig' => 'File size limit is 4MB'],
              ];
          }
      
          public function attributes()
          {
              return ['imageFile'];  
          }
      
      
          public function upload()
          {
              if ($this->validate()) {
      
                  $fileName = $this->imageFile->baseName . '.' . $this->imageFile->extension;
                  $path = $this->documentPath . Yii::$app->user->id . '/';
      
                  if ($this->imageFile->saveAs($path.$fileName) === false)
                      throw new ServerErrorHttpException('Failed for unknown reason. please try again');
      
                  return [
                      'imagePath' => Url::to('@web', true) .'/'. $path.$fileName, 
                  ];
              } 
      
              return false;
          }
      }
      

      【讨论】:

      • 您好,感谢您的快速回复。我已经尝试了上面的代码并在邮递员中进行了测试。它不工作。以下是邮递员尝试使用表单数据的回复。 {“图像文件”:空}。请帮忙??
      • 是的,它应该是 multipart/form-data 而不是 x-www-form-urlencoded。我的代码中的imageFile 是应该上传文件的字段的名称。我认为在您的情况下,该字段称为wretr。还可以尝试取消设置其他字段,我对此了解不多,但[Content-Disposition:_form-data;_name] => "id" 对我来说看起来很奇怪。我可能错了,但我通常将其视为Content-Disposition: form-data; name= "id"
      • 我已经尝试过官方示例和您的示例,但由于某种原因,它们都没有与邮递员一起使用。我继续在 Yii 端获取 imageFile = null 但如果返回 $_FILES 数组,我可以在那里看到我的文件,并且内容类型确实是 multipart/form-data。这真是令人头疼
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多