Uploading file in postman extension

2019-09-10 22:01发布

问题:

I try to upload an image via the postman extension, but I get the following response. Furthermore, the image does not appear in the specified directory.

{
    "name": "PHP Notice",
    "message": "Undefined index: user_id",
    "code": 8,
    "type": "yii\\base\\ErrorException",
    "file": "C:\\xampp\\htdocs\\basic14-4-2016\\controllers\\ApiuploadsController.php",
    "line": 85,
    "stack-trace": [
        "#0 C:\\xampp\\htdocs\\basic14-4-2016\\controllers\\ApiuploadsController.php(85): yii\\base\\ErrorHandler->handleError(8, 'Undefined index...', 'C:\\\\xampp\\\\htdocs...', 85, Array)",
        "#1 [internal function]: app\\controllers\\ApiuploadsController->actionNew()",
        "#2 C:\\xampp\\htdocs\\basic14-4-2016\\vendor\\yiisoft\\yii2\\base\\InlineAction.php(55): call_user_func_array(Array, Array)",
        "#3 C:\\xampp\\htdocs\\basic14-4-2016\\vendor\\yiisoft\\yii2\\base\\Controller.php(151): yii\\base\\InlineAction->runWithParams(Array)",
        "#4 C:\\xampp\\htdocs\\basic14-4-2016\\vendor\\yiisoft\\yii2\\base\\Module.php(455): yii\\base\\Controller->runAction('new', Array)",
        "#5 C:\\xampp\\htdocs\\basic14-4-2016\\vendor\\yiisoft\\yii2\\web\\Application.php(84): yii\\base\\Module->runAction('apiuploads/new', Array)",
        "#6 C:\\xampp\\htdocs\\basic14-4-2016\\vendor\\yiisoft\\yii2\\base\\Application.php(375): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
        "#7 C:\\xampp\\htdocs\\basic14-4-2016\\web\\index.php(12): yii\\base\\Application->run()",
        "#8 {main}"
    ]
}

回答1:

If you want to upload image from postman then you can use this way -

In PostMan

  1. Use POST request
  2. Select form-data radio button
  3. Enter key(image) and select file type right after value textfield to upload file

In Yii2 code

  1. write this code in controller

  2. $image = \yii\web\UploadedFile::getInstanceByName('image');

  3. Move temp file in $image to your server


You'll get all necessary details in $image variable to upload image.