Yii框架webservice上传图像邮递员

问题描述:

我想从邮递员表单数据上传图像。我不能让输入Yii框架webservice上传图像邮递员

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) 

在我的警予编码:

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的':从邮递员

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

响应。但我不能得到输入请求。

我想将post请求输入作为form-data(file)作为数组。

请提前帮助感谢。

$ model-> load()需要具有特定名称的帖子值。所以在你邮递员电话'id'必须是'TblImgUpload [id]'和'图像'必须是'TblImgUpload [image]'。

请注意,'id'和'image'必须作为模型的rules method中的'安全'属性返回,否则将不会以任何大规模方式加载。

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

下面是我为一个REST应用程序使用一次快速的代码片段:

控制器:

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; 
    } 
} 
+0

您好,感谢您快速回复。我已经尝试了上面的代码并在邮差中测试过。它不工作。以下是邮寄员使用表单数据进行的回复。 { “imageFile”:null }。请帮忙?? –

+0

是的,它应该是'multipart/form-data'而不是'x-www-form-urlencoded'。我的代码中的imageFile是应该上传文件的字段的名称。我认为在你的情况下,该字段被称为'wretr'。另外尝试取消其他字段的设置,但我对此知之甚少,但[Content-Disposition:_form-data; _name] =>“id”'对我来说很奇怪。我可能是错的,但我通常将其视为“内容处置:表单数据; name =“id”' –