Php Yii框架webservice在postman中上载图像
我正试图上传来自邮递员表单数据的图像。我无法得到输入Php Yii框架webservice在postman中上载图像,php,web-services,file-upload,yii2,postman,Php,Web Services,File Upload,Yii2,Postman,我正试图上传来自邮递员表单数据的图像。我无法得到输入 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_
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=>“多部分/表单数据”。但我无法获得输入请求
我希望post请求输入作为表单数据(文件)作为数组
请帮忙,谢谢 $model->load()需要具有特定名称的post值。所以在你的邮箱里,“id”必须是“TblImgUpload[id]”,“image”必须是“TblImgUpload[image]”
请注意,“id”和“image”必须作为“safe”属性返回到模型中,否则它们将不会以任何大规模方式加载。使用或检索上载的文件。有一个很好的例子说明如何实现它
下面是我曾经在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;
}
}
型号:
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;
}
}
嗨,谢谢你的快速回复。我已经尝试了上面的代码并在postman中进行了测试。它不起作用。下面是邮递员对表单数据的回应。{“imageFile”:null}。请帮助??是的,它应该是
多部分/表单数据
,而不是x-www-form-urlencoded
<代码>图像文件在我的代码中是应该上载文件的字段的名称。我认为在您的例子中,该字段被称为wretr
。另外,请尝试取消设置其他字段,我对其了解不多,但[Content Disposition:\u form-data;\u name]=>“id”
对我来说看起来很奇怪。我可能错了,但我通常认为它是内容配置:表单数据;name=“id”
我尝试过官方示例和您的示例,但由于某种原因,它们都不能与邮递员一起使用。我一直在Yii端获取imageFile=null,但是如果返回$\u FILES数组,我可以在那里看到我的文件,并且内容类型确实是多部分/表单数据。那真是令人扫兴