2

I have a web form with type=file input for file upload. However, if the user selects a shortcut in windows, it uploads the shortcut file instead of the actual file (shortcut target).

It is possible to detect that the user has selected shortcut - eg. by filename extension "lnk". But how do I (by javascript) automatically upload the target file instead of the shortcut?

Tomas M
  • 6,919
  • 6
  • 27
  • 33
  • javascript can't access filesystem, but - http://stackoverflow.com/questions/1087246/can-javascript-access-a-filesystem – marmeladze Oct 03 '15 at 13:50

1 Answers1

-1
<form onsubmit="return Validate(this);">
  File: <input type="file" name="my file" /><br />
  <input type="submit" value="Submit" />
</form>

Javascript code:

var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"];    
function Validate(oForm) {
    var arrInputs = oForm.getElementsByTagName("input");
    for (var i = 0; i < arrInputs.length; i++) {
        var oInput = arrInputs[i];
        if (oInput.type == "file") {
            var sFileName = oInput.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < _validFileExtensions.length; j++) {
                    var sCurExtension = _validFileExtensions[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }

                if (!blnValid) {
                    alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
                    return false;
                }
            }
        }
    }

    return true;
}
oreopot
  • 3,392
  • 2
  • 19
  • 28
  • Thanks. This code checks file extension. That's not the answer to the question, though. My question is how to upload the actual file which the shortcut points to. – Tomas M Oct 03 '15 at 14:26
  • Downvote is there for "This answer is not useful" cases. That's exactly this case. – Tomas M Oct 05 '15 at 08:04
  • But you are not seeing the Efforts made? :/ – oreopot Oct 05 '15 at 14:12