Last active
July 6, 2020 15:28
-
-
Save eduardodeoh/afe222facb3eaf71b87a74baf1a1cf98 to your computer and use it in GitHub Desktop.
Revisions
-
eduardodeoh revised this gist
Jul 6, 2020 . 1 changed file with 16 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,21 @@ scope "/uploads", FrameworkWeb do pipe_through([:browser_upload]) post("/", UploadsController, :create) end # controller defmodule FrameworkWeb.UploadsController do use FrameworkWeb, :controller def create(conn, params) do content = params["file"].path |> File.stream!() |> Stream.map(& &1) |> Enum.reduce("", fn line, acc -> acc <> line end) send_resp(conn, 200, content) end end # upload.js @@ -58,4 +72,4 @@ Hooks.UploadFile = { }; #.html.leex <%= file_input(:form_upload, :file, phx_hook: "UploadFile") %> -
eduardodeoh revised this gist
Jul 6, 2020 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,6 @@ Hooks.UploadFile = { upload_process .done(function (response_data) { hook.pushEvent("file-uploaded", { "file-content": response_data, "file-name": fileName -
eduardodeoh revised this gist
Jul 6, 2020 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,8 @@ export function UploadFile(formData, url) { } # phx_hook.js import { UploadFile } from "./upload"; Hooks.UploadFile = { mounted() { this.el.addEventListener("change", (e) => { const formData = new FormData(); @@ -57,3 +58,5 @@ Hooks.FileUploadEventLogs = { }, }; #.html.leex <input type="file" name="files[]" phx-hook="UploadFile"> -
eduardodeoh revised this gist
Jul 6, 2020 . 1 changed file with 51 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ #router.ex # Add specific plug pipeline to router.ex pipeline :browser_upload do plug(Plug.Logger) @@ -7,3 +8,52 @@ pipeline :browser_upload do plug(:fetch_live_flash) plug(:put_secure_browser_headers) end # Add route scope "/uploads", FrameworkWeb do pipe_through([:browser_upload]) post("/", UploadsController, :create) post("/event_logs", UploadsController, :event_logs) end # upload.js const jQuery = require("jquery"); export function UploadFile(formData, url) { return jQuery.ajax({ type: "POST", enctype: "multipart/form-data", url: url, data: formData, processData: false, //IMPORTANT! cache: false, contentType: false }); } # phx_hook.js Hooks.FileUploadEventLogs = { mounted() { this.el.addEventListener("change", (e) => { const formData = new FormData(); const fileName = this.el.files[0].name; formData.set("file", this.el.files[0]); const hook = this; let upload_process = UploadFile(formData, "/uploads/your_url"); upload_process .done(function (response_data) { let file_ids = response_data.split("|"); hook.pushEvent("file-uploaded", { "file-content": response_data, "file-name": fileName }); }) .fail(function (response_data) { hook.pushEvent("file-uploaded", { "error": response_data }); }); }); }, }; -
eduardodeoh revised this gist
Jul 6, 2020 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ ##router.ex # Add specific plug pipeline to router.ex pipeline :browser_upload do plug(Plug.Logger) plug(:accepts, ["html"]) -
eduardodeoh revised this gist
Jul 6, 2020 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,10 @@ # Add specific plug pipeline to router.ex pipeline :browser_upload do plug(Plug.Logger) plug(:accepts, ["html"]) plug(:fetch_session) plug(:fetch_live_flash) plug(:put_secure_browser_headers) end -
eduardodeoh renamed this gist
Jul 6, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
eduardodeoh renamed this gist
Jul 6, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ Add specific plug pipeline to router.ex ```code pipeline :browser_upload do plug(Plug.Logger) plug(:accepts, ["html"]) -
eduardodeoh created this gist
Jul 6, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ Add specific plug pipeline to router.ex ```elixir pipeline :browser_upload do plug(Plug.Logger) plug(:accepts, ["html"]) plug(:fetch_session) plug(:fetch_live_flash) plug(:put_secure_browser_headers) end ```