【问题标题】:Input Image File in VueJs + SlimPHP在 VueJs + SlimPHP 中输​​入图像文件
【发布时间】:2018-05-30 08:51:32
【问题描述】:

我是 VueJs 的新手,我想使用 vuejs 输入图像文件和一些文本来创建新用户。到目前为止,我已经制作了base64图像,但是我不知道如何将这个base64图像传递给服务器中的slim code。 这是我的vue代码:

输入文件

<input class="file-input" type="file" name="userPhoto" @change="uploadPhoto">

方法:

uploadPhoto: function(e){

  var reader = new FileReader()
  reader.readAsDataURL(e.target.files[0])

  reader.onload = (e) => {
    this.usr.user_photo = e.target.result
  }
}

帖子:

console.log(this.usr);
this.$http.post('MyAPI', this.usr)
  .then(function(response){
  this.$router.push({path: '/'});
}

console.log 输出这个对象: Image

这是我在苗条帖子请求中的代码

$app->post('/api/user/add', function(Request $request, Response $response){

  //Upload Files
  $directory = $this->get('upload_directory');
  $uploadFiles = $request->getUploadedFiles();

  $uploadedFile = $uploadFiles['userPhoto'];
  if($uploadedFile->getError() === UPLOAD_ERR_OK){
      $filename = moveUploadedFile($directory,$uploadedFile);
      $response->write('Uploaded '.$filename.'<br/>');
  }

  function moveUploadedFile($directory, UploadedFile $uploadedFile){
      $extension = pathinfo($uploadedFile->getClientFilename());
      $basename = bin2hex(random_bytes(8));
      $filename = sprinf('%s.%0.8s',$basename, $extension);

      $uploadedFile->moveTo($directory.'/'.$filename);

      return $filename;
  }

  $user_id = $request->getParam('user_id');
  $password = $request->getParam('password');
  $name = $request->getParam('name');
  $status = $request->getParam('status');
  $prodi = $request->getParam('prodi');
  $social_link = $request->getParam('social_link');

  $sql = "INSERT INTO user 
          VALUES (:user_id,:password,:name,:status,:prodi,CURRENT_TIMESTAMP(),'$filename',:social_link)";

    try{
    // Get DB Object
    $db = new db();
    // Connect
    $db = $db->connect();

    $stmt = $db->prepare($sql);

    $stmt->bindParam(':user_id', $user_id);
    $stmt->bindParam(':password', $password);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':status', $status);
    $stmt->bindParam(':prodi', $prodi);
    $stmt->bindParam(':social_link', $social_link);

    $stmt->execute();

    echo '{"notice" : {"text": "User Added"}}';


    } catch(PDOException $e){
        echo '{"Error": {"text": }'.$e->getMessage().'}';
    }
});

我已经尝试使用 resteasy 并将所有对象复制粘贴到“usr”中,但是我收到了这个错误:

500 内部服务器错误

谁能告诉我怎么做?谢谢

【问题讨论】:

    标签: javascript php vue.js frameworks slim


    【解决方案1】:

    您的 ajax 请求包含 json 编码属性“user_photo”中的图像数据,格式为 Data URI scheme

    这不是“经典”文件上传,所以这段代码不会像预期的那样工作。

    $uploadFiles = $request-&gt;getUploadedFiles();

    改为您可以尝试从 json 数据中读取数据 URI。

    $userPhoto = $request->getParam('user_photo');
    

    然后尝试解码数据URI字符串并保存为文件。

    $userPhoto = str_replace("\n", "", $userPhoto);
    $userPhotoData = substr($userPhoto, strpos($userPhoto, ',') + 1);
    
    // Decode the base64 string
    $userPhotoData = base64_decode($userPhotoData);
    
    // Save the file
    file_put_contents('filename.jpg', $userPhotoData);
    

    注意:切勿在 Slim 中使用 echo。请改用响应对象。

    $result = [
       'notice' => [
            'text' => 'User Added'
        ]
    ];
    
    return $response->withJson($result);
    

    【讨论】:

    • 您好,感谢您的回答。你能给我举例说明如何保存文件吗?到目前为止,我只知道explode和base64_decode,之后我不知道该怎么做了。
    • 嗨,我给你举了个例子。
    • 嗨,谢谢你的例子,我在我的本地主机上试了一下,它可以工作,但我不知道为什么,我在托管服务器上做同样的事情,我得到一个错误“内部服务器错误” ,也许你能帮忙?
    • 很难猜到没有看到错误日志文件。也许您应该检查文件系统的写入权限并将所有错误记录到文件中:akrabat.com/logging-errors-in-slim-3
    • 好的,谢谢你的帮助,稍后会检查
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2012-07-28
    • 2020-11-19
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多