First add in the head tags:
<script>
function showDialog(openFileDialog) {
document.getElementById(openFileDialog).click();
}
function fileName(openFileDialog) {
return document.getElementById(openFileDialog).value;
}
function hasFile(openFileDialog) {
return document.getElementById(openFileDialog).value != "";
}
function fileNameWithoutFakePath(openFileDialog) {
var fileName = document.getElementById(openFileDialog).value;
return fileName.substr(fileName.lastIndexOf('\\') + 1);
}
function fakePathWithoutFileName(openFileDialog) {
var fileName = document.getElementById(openFileDialog).value;
return fileName.substr(0, fileName.lastIndexOf('\\'));
}
</script>
if you already have script tags, just add these functions above.
In your body or form tags adding:
<input type="file" style="display:none" id="yourDesiredOrFavoriteNameForTheNewOpenFileDialogInstance"/>
No matter where in your html, is just like that you've created a new instance of type OpenFileDialog class as global variable, whose name is the id of the element, no matter where in your code or xaml, but in your script or code, you can't type his name, and then read a property or call a function, because there are global functions that do those that are not defined in the element input type="file". You just have to give these functions the id of the hidden input type="file" which is the name of the OpenFileDialog instance as string.
To ease your life in creating open file dialogs instances to your html, you can make a function that does it:
function createAndAddNewOpenFileDialog(name) {
document.getElementById("yourBodyOrFormId").innerHtml += "<input type='file' style='display:none' id='" + name + "'/>"
}
and if you want to remove open file dialog, then you can make and use the following function:
function removeOpenFileDialog(name) {
var html = document.getElementById("yourBodyOrFormId").innerHtml;
html = html.replace("<input type='file' style='display:none' id='" + name + "'/>", "");
document.getElementById("yourBodyOrFormId").innerHtml = html;
}
but before you remove open file dialog, ensure that it exists by making and using the following function:
function doesOpenFileDialogExist(name) {
return document.getElementById("yourBodyOrFormId").innerHtml.indexOf("<input type='file' style='display:none' id='" + name + "'/>") != -1
}
and if you don't want to create and add the open file dialogs in the body or form tags in the html, because this is adding hidden input type="file"s, then you can do it in script using the create function above:
function yourBodyOrFormId_onload() {
createAndAddNewOpenFileDialog("openFileDialog1");
createAndAddNewOpenFileDialog("openFileDialog2");
createAndAddNewOpenFileDialog("openFileDialog3");
createAndAddNewOpenFileDialog("File Upload");
createAndAddNewOpenFileDialog("Image Upload");
createAndAddNewOpenFileDialog("bla");
//etc and rest of your code
}
Ensure that near your body or form tags, you added:
onload="yourBodyOrFormId_onload()"
You don't have to do this line above, if you did it already.
TIP: You can add to your project or website new JScript File, if you don't have yet, and in this file you can put all the open file dialog functions away from the script tags and the html or web form page, and use them in your html or web form page from this JScript file, but don't forget before to link the html or web form page to the JScript File of course. You can do it just by dragging the JScript file to your html page in the head tags. If your page is web form and not simple html, and you don't have head tags, then put it anywhere so that it can work.
Don't forget to define global variable in that JScript File, whose value will be your body or form id as string. After you linked the JScript file to your html or web form page, you can onload event of your body of form, set the value of that variable to your body or form id. Then in the JScript File, you don't have to give to the document the id of the body or form of one page anymore, just give it the value of that variable. You can call that variable bodyId or formId or bodyOrFormId or any other name you want.
Good luck man!