0

I use a small file upload dialog to show some images.

$("document").ready(function () {
    $("#dialogImageUpload").change(function (input) {
        $(input.target.files).each(function (imageIndex, currentImage) {
            if (currentImage.type.match('image.*')){
                alert(currentImage.name);
            }
        });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="file" id="dialogImageUpload" multiple>
    <div id="ImageListContainer"></div>

So you can read the name of the currentImage. How can I get the full path from it?

For example

var uploadedImage = $("<div><img src= theFilePath /></div>");

The second question I have is when using the file dialog, the change event obviously gets triggered if different files are selected as before. But what if I want to upload a file twice? The change event is not triggered anymore. Can I solve this somehow?

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
peterHasemann
  • 1,410
  • 2
  • 15
  • 41
  • If you mean that you want to get the full client side path of the file, then you cannot. It's blocked for security reasons. You can get the file name directly from the input element, though *Edit* - in fact your code is already doing this, so I'm confused what you're asking here. – Rory McCrossan Aug 15 '17 at 10:04
  • Well I got the file name but the image path is missing :/ Means I need to fill the image source with the full path – peterHasemann Aug 15 '17 at 10:11
  • Assuming you want to preview the image that's selected you don't do it via the path, you use the binary data of the file. See this question for more info: https://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – Rory McCrossan Aug 15 '17 at 10:12
  • [Filereader - upload same file again not working](https://stackoverflow.com/questions/26634616/filereader-upload-same-file-again-not-working) – adeneo Aug 15 '17 at 10:14

1 Answers1

1
  1. You can not get the file path by javascript, because of the security limit by browser.

  2. You can change the value to '' by JS when you click on the file input, so it will trigger onchange if you choose anything.

LIXer
  • 282
  • 4
  • 10