R – How to get FullName with path info in FireFox using asp.net upload control

cross-browserfile uploadfirefoxinternet explorer

How to get FullName with path info in FireFox using asp.net upload control?

With IE, I can get the FullName of a file with the full path info using asp.net upload control:

<asp:FileUpload ID="FileUpload1" runat="server" />

In IE, the FileUpload1.PostedFile.FileName is E:\iProject\Demo1\abc.jpg

But in FireFox, the FileUpload1.PostedFile.FileName is abc.jpg

How can I get the upload file's fileName with full path info when I use FireFox?

I want to use the path info of the file, so I can upload the file to the same folder automatically.

Or can I use javascript to get the path info on the uploadfile field's onchange() event?

Best Answer

You can't. This is a deliberate security measure.

In fact you can't really rely on any given browser giving you anything reasonable as a file name, so it's a good idea to prompt the user for a name to use in a separate input control. You can use some JavaScript to make it default to the filename where available, by reading the file upload field's value onchange, and copying the last segment post '/' or '\' if there is one to the name field.

example added re comment:

<input type="text" name="filename" id="filename" />
<input type="file" name="upload" id="upload" />
<script type="text/javascript">
    document.getElementById('upload').onchange= function() {
        var leafname= this.value.split('/').pop().split('\\').pop();
        if (leafname!='')
            document.getElementById('filename').value= leafname;
    };
</script>