0

I have an image upload. When selecting some files from a filedialog, the files get added to an array and a new div container is added to the page. This div container contains the image and a delete button.

var imageList = [];

$("document").ready(function () {
    var dlgImageUpload = $("#inputUpload"); // the input dialog
    dlgImageUpload.click(function(){this.value = '';}); // reset the value
    dlgImageUpload.change(function (input) { // new images selected
        $(input.target.files).each(function (imageIndex, currentImage) {
            if (currentImage.type.match('image.*')){ // just execute the code if the file is an image
                addNewImage(currentImage.name); // handle the array and create a new container
            }
        });
    });
});

function initializeImageList(){ // load images already existing (from server?)
    $(imageList).each(function (imageIndex) {
        createImageContainer(imageIndex);
    });
}

function addNewImage(fileName){ // add the new image to the list and create it at the first place in the DOM
    imageList.unshift(fileName);
    createImageContainer(0);
}

function createImageContainer(imageListIndex){ // create a new div container for the image
    var uploadContainer = $("<div></div>"); // the parent container
    uploadContainer.addClass("uploadContainer");
    $('#ImageListContainer').append(uploadContainer);
    $(uploadContainer).insertBefore($(".uploadContainer")[0]);

    var btnBar = $("<div></div>"); // containing the delete button and other ones
    btnBar.addClass("uploadBtnBar");
    uploadContainer.append(btnBar);

    var btnDelete = $("<button></button>"); // the button for deleting the current image
    btnDelete.addClass("btn btnDark btnDeleteImage");
    btnDelete.click(function () {
        imageList.splice(imageListIndex, 1);
        uploadContainer.remove();
    });
    btnBar.append(btnDelete);

    var uploadedImage = $("<div></div>"); // the image container itself with a background image
    $(uploadedImage).css("background-image", "url('Resources/" + imageList[imageListIndex] + "')");
    uploadedImage.addClass("uploadedImage");
    uploadContainer.append(uploadedImage);
}
#ImageListContainer{
    margin-top: 50px;
}

#inputUpload{
    margin-left: 20px;
}

.uploadContainer{
    display: inline-block;
    vertical-align: middle;
    position: relative;
    width: 200px;
    height: 200px;
    margin: 20px;
}

.uploadContainer:hover .uploadBtnBar{
    display: block;
}

.uploadContainer:hover .uploadedImage{
    opacity: 0.3;
    transition: 1s;
}

.uploadedImage{
    width: 100%;
    height: 100%;
    background-size: cover;
    transition: 1s;
}

.uploadBtnBar{
    position: absolute;
    z-index: 1;
    display: none;
    width: 100%;
    text-align: right;
    height: 32px;
}

.btnDeleteImage{
    width: 32px;
    height: 32px;
    background-image: url("../../Resources/deleteImageBtn.png");
    background-size: cover;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="imageUpload" class="contentPage">
    <input class="input" id="inputUpload" type="file" multiple>
    <div id="ImageListContainer"></div>
</div>

and as you can see in the example mentioned above, the images are not found. So when writing $(uploadedImage).css("background-image", "url('Resources/" + imageList[imageListIndex] + "')");

My url is currently wrong. I search for existing images in this Resources folder. How can I upload images from where I want?

I know I can not read the path of the file due to security reasons. But how can I solve this problem?

The rest of the code should be fine, I just need to get the right url from the selected file.

peterHasemann
  • 1,550
  • 4
  • 24
  • 56
  • Have you tried converting the image to a base64 encoded string? (https://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript) – CaptainCarl Aug 21 '17 at 14:30
  • No, I don't know how this works. I would have to look it up – peterHasemann Aug 21 '17 at 14:31
  • You can not get the local file path, for security/privacy reasons. Your options are either to use a JavaScript FileReader to read the image data on the client side; or having to upload the image to the server first, so that i can then render and return an appropriate thumbnail. – CBroe Aug 21 '17 at 14:38
  • @CBroe yes I will follow CaptainCarls instructions, this topic seems confusing to me :S – peterHasemann Aug 21 '17 at 14:39

0 Answers0