2017-05-30 37 views
0

我想從郵遞員表單數據上傳圖像。我不能讓輸入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)作爲數組。

請提前幫助感謝。

回答

0

$ model-> load()需要具有特定名稱的帖子值。所以在你郵遞員電話'id'必須是'TblImgUpload [id]'和'圖像'必須是'TblImgUpload [image]'。

請注意,'id'和'image'必須作爲模型的rules method中的'安全'屬性返回,否則將不會以任何大規模方式加載。

0

使用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」' –