How to get FullName with path info in FireFox usin

2019-08-25 18:26发布

问题:

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?

回答1:

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>


回答2:

Firefox does not allow you to know that information. Doing so gives the server extra knowledge of the client (security risk) that it doesn't need.

Honestly, why would you need it? The file is going to be uploaded to your server anyway, right?



回答3:

I think it is a good practice not send the full path name, it raises privacy and security concerns. The whole pathname says too much about the directory structure, which can be used by attackers. Maybe you can't get the full path at all in Firefox.



回答4:

But, can you imagine, that i've written a signed java applet that can send files of unlimited size in fragments, in an Ajax postback. And it works perfectly with IE. Fails with FF. The only way is to tell FF users, that my website should be viewed only in IE.