Skip to content

Instantly share code, notes, and snippets.

@michalkorzawski
Last active October 30, 2022 22:11
Show Gist options
  • Save michalkorzawski/9f15ecfa8d7432aadd3c9bbf95eda849 to your computer and use it in GitHub Desktop.
Save michalkorzawski/9f15ecfa8d7432aadd3c9bbf95eda849 to your computer and use it in GitHub Desktop.
// app/javascript/controllers/image_preview_controller.js
import ApplicationController from "./application_controller";
export default class extends ApplicationController {
static targets = ["imagePreview"];
...
renderImagePreview(file) {
const formData = new FormData();
formData.append("image", file);
fetch("/convert_heic", {
method: "post",
body: formData
}).then(response => {
response.blob().then(imgBlob => {
let blobURL = URL.createObjectURL(imgBlob);
this.imagePreviewTarget.insertAdjacentHTML("beforeend", `<img src="${blobURL}">`);
}).catch(e => {
// logic to show alert flash msg
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment