如何上传预览的图像

时间:2018-12-07 16:23:16

标签: javascript php jquery

我有此表格 Previewed images

如何通过按void makemap( map * game ){ int ychange; int flag = 0; initscr(); cbreak(); noecho(); getmaxyx( stdscr, maxy, maxx ); mvprintw(0, maxx - (maxx - 4), "Enter 'q' to quit at end of attack, or control-C"); refresh(); for( size_t i = 0; i < game->size; i++ ){ ychange = game->layout[i] - game->layout[i+1]; if(game->layout[i] > maxheight) maxheight = game->layout[i]; if(game->layout[i+1] > maxheight) maxheight = game->layout[i+1]; if( ychange == 0 ){ mvprintw(maxy - game->layout[i], i, "_"); refresh(); } else if( ychange < 0 ){ for( int c = 0; c < game->layout[i+1]-2; c++ ){ mvprintw(maxy - 2 - c , i, "|"); refresh(); } } else if( ychange > 0 ){ for( int c = 0; c < game->layout[i] - 2; c++ ){ mvprintw(maxy - 2 - c, i ,"|"); refresh(); } } } if( (int)game->size < maxx ){ if( !(game->layout[game->size] == 2) ){ flag = 1; for( int i = 0; i < game->layout[game->size] - 2; i++ ){ mvprintw(maxy - 2 - i, game->layout[game->size] + 1, "|"); refresh(); } } if( flag == 1 ){ for( int i = game->size; i <= maxx; i++ ){ mvprintw(maxy - 2, i, "_"); refresh(); } } else{ for( int i = game->size; i < maxx; i++ ){ mvprintw(maxy - 2, i, "_"); refresh(); } } } 来获取所观看图片的名称。 我需要预览图像的名称以将其保存到数据库。 我还想限制用户最多添加5张图像的功能。

谢谢。

我的表格代码:

DODAJ button

用于图像预览的JQUERY:

<form action="uploadProduct.php" method="post">
                <div class="modal-content">
                    <!--Header-->
                    <div class="modal-header">
                        <p class="heading lead">Dodavanje proizvoda</p>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true" class="white-text">×</span>
                        </button>
                    </div>

                    <!--Body-->
                    <div class="modal-body">
                        <p class="text-center">
                            <strong>Odaberite katogoriju i ispunite prazna polja.</strong>
                        </p>
                        <hr>
                        <select name="kategorija" id="category" class="browser-default custom-select">
                            <option valve="0" selected>Odabir kategorije</option>
                            <option value="1">Poljoprivredni strojevi</option>
                            <option value="2">Šumarski strojevi</option>
                            <option value="3">Ostalo</option>
                        </select>

                        <div id="product1" style="display:none">
                            <div class="md-form">
                                <input type="text" name="naziv" class="form-control" required>
                                <label for="product_name">Unesite naziv proizvoda</label>
                            </div>
                            <div class="md-form">
                                <textarea type="text" name="opis" class="md-textarea form-control" rows="3"></textarea>
                                <label for="product_specif">Opis proizvoda</label>
                            </div>
                            <div class="md-form">
                                <input id="fileUpload" multiple="multiple" type="file"/> 
                                <div id="image-holder"></div>
                            </div>
                        </div>
                    </div>

                    <!--Footer-->
                    <div class="modal-footer justify-content-right">
                        <a type="button" class="btn btn-outline-primary waves-effect" data-dismiss="modal">Odustani</a>
                        <button type="submit" name="productSubmit" class="btn btn-primary waves-effect waves-light">Dodaj</button>
                    </div>
                </div>
            </form>

要上传的PHP代码:

$(document).ready(function() {
            $("#fileUpload").on('change', function() {
                //Get count of selected files
                var countFiles = $(this)[0].files.length;
                var imgPath = $(this)[0].value;
                var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
                var image_holder = $("#image-holder");
                image_holder.empty();
                if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
                    if (typeof(FileReader) != "undefined") {
                    //loop for each file selected for uploaded.
                    for (var i = 0; i < countFiles; i++) 
                    {
                        var reader = new FileReader();
                        reader.onload = function(e) {
                        $("<img />", {
                            "src": e.target.result,
                            "class": "thumb-image"
                        }).appendTo(image_holder);
                        }
                        image_holder.show();
                        reader.readAsDataURL($(this)[0].files[i]);
                    }
                    } else {
                    alert("This browser does not support FileReader.");
                    }
                } else {
                    alert("Pls select only images");
                }
            });
        });

2 个答案:

答案 0 :(得分:0)

<html>
<head>
<style type="text/css">
.thumb-image{
 float:left;width:100px;
 position:relative;
 padding:5px;
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<form action="uploadProduct.php" method="post">
<div id="wrapper" style="margin-top: 20px;"><input id="fileUpload" multiple="multiple" type="file"/> 
<div id="image-holder"></div>
<button type="submit" name="productSubmit">Upload</button>
</div>
</form>

<script>
$(document).ready(function() {
        $("#fileUpload").on('change', function() {
                    //Get count of selected files
                    var countFiles = $(this)[0].files.length;
                    var imgPath = $(this)[0].value;
                    var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
                    var image_holder = $("#image-holder");
                    image_holder.empty();
                    if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
                        if (typeof(FileReader) != "undefined") {
                        //loop for each file selected for uploaded.
                        for (var i = 0; i < countFiles; i++) 
                        {
                            var reader = new FileReader();
                            reader.onload = function(e) {
                            $("<img />", {
                                "src": e.target.result,
                                "class": "thumb-image"
                            }).appendTo(image_holder);
                            }
                            image_holder.show();
                            reader.readAsDataURL($(this)[0].files[i]);
                        }
                        } else {
                        alert("This browser does not support FileReader.");
                        }
                    } else {
                        alert("Pls select only images");
                    }
                });
      });
</script>
</body>
</html>

单击“上传”后添加图像后,我想获取预览图像的名称并将其发送到upload.php。

答案 1 :(得分:0)

一种方法(我并不是说这是最好的方法)是在#fileUpload onchange中创建一个带有该按钮的表单。

例如

 <form action="/upload_page.php">
     <input type="hidden" name="category"    value="Mickey">
     <input type="hidden" name="file_name_1" value="Mouse">
     <input type="submit" value="Dodaj">
 </form>