1

I'm doing my first web app using nodeJS with expressJS, I'm trying to upload an image to my folder "uploads", and it's work, with multer, it's uploaded, but it isn't an image it's look like 3c1dgn4vcs3c33vd, I've seek on a lot of forums but I didn't find an answer for helping me, I don't know how can I display this image on my views ?_? You can check on my github (https://github.com/sovada/Harmony_Dev) the file who work with upload file is that: controller/adminController.js:81

app.post("/hd-admin/blog/added", multer({dest: "./assets/uploads"}).single("img"), function (req, res) {
    var titleEN   = req.body.titleEN,
        titleFR   = req.body.titleFR,
        titleES   = req.body.titleES,

        contentEN = req.body.contentEN,
        contentFR = req.body.contentFR,
        contentES = req.body.contentES,

        img       = "img/" + req.file.originalname,
        date      = moment().format("DD/MM/YYYY"),
        author    = "Harmony_dev",
        category  = req.body.category,
        url       = req.body.URL;

    var add = new blog ({
        titleEN     : titleEN,
        titleFR     : titleFR,
        titleES     : titleES,
        contentEN   : contentEN,
        contentFR   : contentFR,
        contentES   : contentES,
        img         : img,
        date        : date,
        author      : author,
        category    : category
    }).save(function (err, data) {
        !err ? res.redirect("/hd-admin/blog") : console.log("err");
    });
});

And the view with the form : views/admin/blog/add.ejs:61.

<form method="post" action="/hd-admin/blog/added" class="form-to-add-post" enctype="multipart/form-data">
                <input type="text" name="titleEN" placeholder="title english"/>
                <input type="text" name="titleFR"  placeholder="title french"/>
                <input type="text" name="titleES"  placeholder="title spanish"/>

                <textarea name="contentEN">Content english</textarea>
                <textarea name="contentFR">Content french</textarea>
                <textarea name="contentES">Content spanish</textarea>

                <input type="file" name="img"/>
                <input type="text" name="category"  placeholder="category"/>
                <input type="text" name="URL"  placeholder="url"/>

                <input type="submit" />
            </form>

And the view who display the images :

<article class="post">
        <img src="/<%= data.img %>" alt="<%= data.title %>" title="<%= data.title %>"/>

        <% if (lang === "en") { %>
        <h1><%= data.titleEN %></h1>
        <p><%= data.contentEN %></p>
        <% } %>
        <% if (lang === "fr") { %>
        <h1><%= data.titleFR %></h1>
        <p><%= data.contentFR %></p>
        <% } %>
        <% if (lang === "es") { %>
        <h1><%= data.titleES %></h1>
        <p><%= data.contentES %></p>
        <% } %>

        <div class="link">
            <span><i class="pe-7s-user"></i> <%= data.author %></span>
            <span><i class="pe-7s-date"></i> <%= data.date %></span>
            <span><i class="pe-7s-folder"></i> <%= data.category %></span>
        </div>
    </article>

Thank's you, really.

  • you have to show us the code you are using to handle image uploads. – zola Nov 02 '15 at 14:27
  • thank's, I juste do "find" and I send the result with render in my views : controller/blogController.js:35 and in my views I do <%= data.img %> – Beeckman Jeremy Nov 02 '15 at 14:34
  • Your question is really hard to read, you should provide involved code blocks here, not the links to you GH account if you want ppl to dive into your problem. Aside from that, it seems that you want to find the local server image url in data.img parameter, but you don't save it there on successful upload – shershen Nov 02 '15 at 14:42
  • Oh I'm sorry I was thinking it will be more easy with the GH, sorry, I wrote the code. – Beeckman Jeremy Nov 02 '15 at 14:49
  • I found something, when I rename the strange file (sudo mv 4bk5fdn64sbcds45 test.jpg) it's an image, I think I just have to rename all of this images after they are uploaded. – Beeckman Jeremy Nov 02 '15 at 15:16

0 Answers0