Skip to content

Instantly share code, notes, and snippets.

@eduardodeoh
Last active July 6, 2020 15:28
Show Gist options
  • Select an option

  • Save eduardodeoh/afe222facb3eaf71b87a74baf1a1cf98 to your computer and use it in GitHub Desktop.

Select an option

Save eduardodeoh/afe222facb3eaf71b87a74baf1a1cf98 to your computer and use it in GitHub Desktop.

Revisions

  1. eduardodeoh revised this gist Jul 6, 2020. 1 changed file with 16 additions and 2 deletions.
    18 changes: 16 additions & 2 deletions lv_upload.ex
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,21 @@ scope "/uploads", FrameworkWeb do
    pipe_through([:browser_upload])

    post("/", UploadsController, :create)
    post("/event_logs", UploadsController, :event_logs)
    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
    <input type="file" name="files[]" phx-hook="UploadFile">
    <%= file_input(:form_upload, :file, phx_hook: "UploadFile") %>
  2. eduardodeoh revised this gist Jul 6, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion lv_upload.ex
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,6 @@ Hooks.UploadFile = {
    upload_process
    .done(function (response_data) {
    let file_ids = response_data.split("|");
    hook.pushEvent("file-uploaded", {
    "file-content": response_data,
    "file-name": fileName
  3. eduardodeoh revised this gist Jul 6, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion lv_upload.ex
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,8 @@ export function UploadFile(formData, url) {
    }

    # phx_hook.js
    Hooks.FileUploadEventLogs = {
    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">
  4. eduardodeoh revised this gist Jul 6, 2020. 1 changed file with 51 additions and 1 deletion.
    52 changes: 51 additions & 1 deletion lv_upload.ex
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    ##router.ex
    #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 });
    });
    });
    },
    };

  5. eduardodeoh revised this gist Jul 6, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions lv_upload.ex
    Original 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"])
  6. eduardodeoh revised this gist Jul 6, 2020. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions lv_upload.ex
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@
    Add specific plug pipeline to router.ex
    # Add specific plug pipeline to router.ex


    ```code
    pipeline :browser_upload do
    plug(Plug.Logger)
    plug(:accepts, ["html"])
    plug(:fetch_session)
    plug(:fetch_live_flash)
    plug(:put_secure_browser_headers)
    end
    ```
  7. eduardodeoh renamed this gist Jul 6, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. eduardodeoh renamed this gist Jul 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt → lv
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Add specific plug pipeline to router.ex

    ```elixir
    ```code
    pipeline :browser_upload do
    plug(Plug.Logger)
    plug(:accepts, ["html"])
  9. eduardodeoh created this gist Jul 6, 2020.
    11 changes: 11 additions & 0 deletions gistfile1.txt
    Original 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
    ```