[英]How to Save Image in mysql database using angular2 and php
我想将图像保存在mysql数据库中。 我如何使用php和angular做到这一点?
我正在使用add-style.component.ts上传图像
imageUploadEvent(event){
let imageFile = event.target;
if(imageFile.files.length > 0){
console.log(imageFile.files[0]);
let formData = new FormData();
formData.append('file',imageFile.files[0]);
this._databaseService.insertImageData(formData).subscribe((msg) => {
console.log(msg);
},(error) =>{
console.log("Get Some Error");
console.log(error);
});
}
}
我使用add-style.html如下
<div class="container">
<div class="row">
<div class="col s8 offset-2">
<div class="card-panel teal lighten-2">
<h4>Image Uploading</h4>
</div>
<form #imageform ="ngForm">
<input type="file" class="form-file-control" (change)="imageUploadEvent($event)">
<button type="submit" class="btn-lg btn-submit form-control" (click)="saveFormData()" >submit</button>
</form>
</div>
</div>
</div>
我使用database.service.ts调用php
insertImageData(value){
return this._http.get("http://localhost/jeleena-api/test-img.php",value).pipe(map(res => {
console.log(res);
}));
}
以下是我的PHP代码
<?php
//Create Database Connection
include "db-connection.php";
if(!empty($_FILES['file'])){
$ext = pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
$image = time().'.'.$ext;
if(move_uploaded_file($_FILES["file"]["tmp_name"],$image)){
$sql = "INSERT INTO test(img) VALUES('".$image."')";
$conn->query($sql);
echo "successfully uploaded";
}
else{
echo ' error ';
}
}
?>
好吧,我承担。 您可以为此使用ngStyle。 https://angular.io/api/common/NgStyle您需要在ngModule中安装commonsmodule。
现在,您可以在模板中执行以下操作以显示图像:
<div [ngStyle]="background-image: url('./assets/images/youImage.jpg');">
现在,您可以基于URL显示图像了。 因此,您可以做的就是将图像保存到sql中,而不是将图像保存到sql中,只需简单地保存./assets/images/youImage.jpg
。
<div [ngStyle]="background-image: url(yourObject.image)>
为了显示图像,您可以使一个对象为其赋予属性图像,并从sql中分配字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.