如何解决如何在 PHP + slim 3 API 中保存 ngx-dropzone 以角度上传的文件?
在 angular 中,我使用 ngx-dropzone 上传图像。到目前为止一切顺利。
要选择我使用的图像:
onSelect(event): void {
this.files.push(...event.addedFiles);
}
然后,为了将我的图像上传并保存在磁盘上,我使用此功能:
uploadFile() {
const formData = new FormData();
for (let i = 0; i < this.files.length; i++) {
formData.append('file[]',this.files[i]);
}
this.httpService.post<any>('http://localhost:8080/upload-photo',formData)
.subscribe(res => {
console.log('result :',res)
});
}
这有效。
我认为问题出在背面,我用的是slim 3的php。
这是我用来处理上传的代码:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: multipart/form-data; charset=UTF-8");
header("Access-Control-Allow-Methods: GET,POST,OPTIONS,PUT,DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Origin,X-Requested-With,Content-Type,Accept");
$app->post('/upload-photo',function (\Slim\Http\Request $request,\Slim\Http\Response $response,array $args) {
$folderPath = "upload/";
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
foreach ($request->fileSource as $key => $value) {
$image_parts = explode(";base64,",$value);
$image_type_aux = explode("image/",$image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$file = $folderPath . uniqid() . '.' . $image_type;
file_put_contents($file,$image_base64);
}
});
这不起作用,文件未保存在“上传”文件夹中,并且从 post 调用返回为空,没有错误。
我也很恼火,因为我不知道如何调试后端发生的事情。我知道邮递员,但我不知道如何将实际从前端发送的数据发送给它。
非常感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。