0

I am uploading a file in my application using a html file upload control now. Instead of file upload control,on selecting checkbox controls I need to fetch the file from local disk.I am using Javascript code in my application.Can Anyone help me out in this.

Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
Smith
  • 17
  • 6

2 Answers2

0

You can use the FormData javascript object https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects but just modern browsers will have that one. For older browsers you can use a iframe How to make Asynchronous(AJAX) File Upload using iframe?

Community
  • 1
  • 1
Wilmer
  • 138
  • 1
  • 8
0

You can use a hidden file upload control and manipulate it using JavaScript. I use the code below to allow the user to select a profile image and see it in place before uploading. I use a link, but you can change it to a checkbox like you requested:

function LoadImage() {
  document.getElementById("ImageFile").click();
}

function DisplayImage(FileBrowser) {
  if (FileBrowser.files.length < 1)
    return;
  var file = FileBrowser.files[0].name;
  if (file == null || file == "")
    return;
  var reader = new FileReader();
  reader.onload = function() {
    document.getElementById("ImageDisplay").src = this.result;
  };
  reader.readAsDataURL(FileBrowser.files[0]);
}
<input id="ImageFile" type="file" style="visibility: hidden; height : 0; width: 0;" onchange="DisplayImage(this);" />
<a href="javascript: void LoadImage();">
  <img id="ImageDisplay" alt="Select an image" src="" />
</a>
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • Thanks for the code.I just want to avoid the manual selection of the file.Instead I want to select the file as a back end process.Is this possible.The file path and file name will be static. – Smith Jul 08 '16 at 13:11
  • Selecting a file MUST be a manual process because web browsers don't have access to the file system for security and privacy reasons. The only way to do it in a web browser is to include a binary executable such as a Java Applet which can access the file system, but it has to be trusted by the user. – Racil Hilan Jul 08 '16 at 14:22