简体   繁体   English

使用HTML,JS和MONGODB上传和显示图像

[英]Upload and Display Images using HTML, JS and MONGODB

I have managed to load the image on my page using HTML and JS but I can't seem to post the image to my MongoDB and it won't show on my 'home' page. 我已经设法使用HTML和JS将图像加载到页面上,但似乎无法将图像发布到MongoDB上,并且该图像也不会显示在“主页”页面上。

HTML: HTML:

<div class="container">
    <h1 style="text-align: center">Post a New Guitar</h1>
    <div class="col-md-6">
        <form action="/guitars" method="POST">
        <div class="input-group">
            <label>Item Name</label>
            <div class="input-group">
                <input class="form-control" type="text" name="name" placeholder="Name">
            </div>
            <label>Upload Image</label>
            <div class="input-group">
        <span class="input-group-btn">
            <span class="btn btn-default btn-file">
                Browse… <input type="file" id="imgInp">
            </span>
        </span>
        <input type="text" class="form-control" readonly>
    </div>
    <img id="img-upload"/>

      <div class="input-group">
            <button class="btn btn-lg btn-primary btn-block">Submit!</button>
        </div>

    <a href="/guitars">Go Back</a>
</div>

JS: JS:

        $(document).ready( function() {
            $(document).on('change', '.btn-file :file', function() {
               var input = $(this),
        label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
    input.trigger('fileselect', [label]);
    });

    $('.btn-file :file').on('fileselect', function(event, label) {

        var input = $(this).parents('.input-group').find(':text'),
            log = label;

        if( input.length ) {
            input.val(log);
        } else {
            if( log ) alert(log);
        }

    });
    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#img-upload').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imgInp").change(function(){
        readURL(this);
    });     
});

When submitting the form, I want it to display the image that has been uploaded using JS and for it to be 'saved' to my database. 提交表单时,我希望它显示使用JS上传的图像,并将其“保存”到我的数据库中。

What do you mean by "post the image to MongoDB"? “将映像发布到MongoDB”是什么意思? You shouldn't be saving image in the database. 您不应该在数据库中保存图像。

What you want to do is when you upload in image, store it in a publicly accessible location in your server. 您要做的就是上传图像时,将其存储在服务器中可公开访问的位置。 What you save in the database is not the image itself but the location of the image on the server. 您保存在数据库中的不是映像本身,而是映像在服务器上的位置。

Image stored in the server: /home/public/image.jpg 存储在服务器中的图像: /home/public/image.jpg

Image path stored in Mongo DB: { path: '/home/public/image.jpg' } 存储在Mongo DB中的图像路径: { path: '/home/public/image.jpg' }

When you go back to your html page, you just need to query the path of the image in Mongo DB and use that value as a source in your image tag. 当您返回html页面时,您只需要在Mongo DB中查询图像的路径,并将该值用作图像标记中的源即可。

`<img src="${path}" />`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM