0

I want get the full path image from input file for show image preview and use for example attr of jquery for insert this into scr to this temporal image path , for example i think in that

var filePath = $(this).val();
console.log(filePath);

jQuery('#preview').attr("src",""+img_p);

The problem i don´t know how i can get this temporal path from input file for show and insert this path for the preview image until send to upload in the system

Thank´s , Regards

user2501504
  • 191
  • 3
  • 6
  • 15
  • I think if you can retrieve the absolute URL of the image, using regex you can cut out parts of the url which should leave you with the relative part. – Shivam Aug 09 '13 at 03:50

1 Answers1

0

MOZILLA DEVELOPER NETWORK show us an example to do that:

<input type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)">
<a href="#" id="fileSelect">Select some files</a> 
<div id="fileList">
  <p>No files selected!</p>
</div>

<script>
window.URL = window.URL || window.webkitURL;

var fileSelect = document.getElementById("fileSelect"),
    fileElem = document.getElementById("fileElem"),
    fileList = document.getElementById("fileList");

fileSelect.addEventListener("click", function (e) {
  if (fileElem) {
    fileElem.click();
  }
  e.preventDefault(); // prevent navigation to "#"
}, false);

function handleFiles(files) {
  if (!files.length) {
    fileList.innerHTML = "<p>No files selected!</p>";
  } else {
    var list = document.createElement("ul");
    for (var i = 0; i < files.length; i++) {
      var li = document.createElement("li");
      list.appendChild(li);

      var img = document.createElement("img");
      img.src = window.URL.createObjectURL(files[i]);
      img.height = 60;
      img.onload = function(e) {
        window.URL.revokeObjectURL(this.src);
      }
      li.appendChild(img);

      var info = document.createElement("span");
      info.innerHTML = files[i].name + ": " + files[i].size + " bytes";
      li.appendChild(info);
    }
    fileList.appendChild(list);
  }
}
</script>

HERE the Mozilla DOC.

HERE some problem to do that.

Alex Ball
  • 3,886
  • 2
  • 14
  • 21