I have a news article component which as a template, has four different images attached to it: Header, Avatar, and Featured Image and Advertising Banner. I am writing in Angular. The images are posted to a files endpoint (which is the files directory) and once I upload them, the four images need to be defined as key values within the news article object: 'header_image', 'author_avatar', 'featured_image' and 'advertising_banner'.
I pass the image file and key directly into the upload function like this in order to ensure I get the right kv from the image upload. When console logged, the data blob for the image logs and so does the key correctly, so they are defined in some way:
.component.html
<input type="file" name="header_image" (change)="fileProgress($event)"
[(ngModel)]="header_image" accept=".jpg,.svg,.png,.jpeg">
<button class="update" (click)="uploadImage('header_image', header_image)">Upload</button>
.component.ts
uploadImage(key: string, imageFile) {
if (null) {
return alert('Please choose an image first')
}
imageFile = this.fileData;
let reader = new FileReader();
reader.readAsDataURL(imageFile);
reader.onload = () => {
let data = reader.result;
this.configService.uploadImage({
title: imageFile.name,
name: imageFile.name,
type: imageFile.type,
size: imageFile.size, data
}).subscribe(response => {
imageFile = response.data;
console.log(key, imageFile)//key values print exactly as expected
});
}
};
article.ts
import { Image } from "../settings/image";
import { Feed } from "./feed";
export class Article {
id: number;
feed_type: {
data: Feed;
}
title: string;
description: string;
text_content: string;
author_name: string;
author_avatar: {
data: Image; //how do you post this image to files?
}
video_url: string;
header_image: Image; //and this image to files?
featured_image: Image; //and this image to files?
twitter: string;
facebook: string;
instagram: string;
advertising_banner: {
data: Image; //and also this image to files at the time when createArticle() is clicked?
}
ad_link: string;
likes: number;
shares: number;
views: number;
}
image.ts
export interface Image {
id: number;
date_uploaded: Date;
url: string;
name: string;
title: string;
}
The user journey when creating an article is as follows:
- user writes text
- user picks images and uploads them
- user publishes the article
Once the images have been uploaded via the uploadImage() function, there is a final create article button which triggers this:
createArticle(article) {
this.articleService.postArticle(article)
.subscribe(
response => {
this.startLoading();
if (response) {
this.article = response.data;
this.stopLoading();
} else {
this.stopLoading();
}
}
)
}
This then posts the article object to the articles array on the server, but the images are not going up with it. I have come to SO to see if anyone has created something similar to this before and has a trick they could share to ensure that once the images have been uploaded, I can then model them to the individual image keys within the article object. Then send the whole object up as a new news article object.