views:

1170

answers:

4

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  A: 

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>
bobince
How can I use the file upload field's value onchange to get the path info? javascript?
Mike108
added example of getting the filename. You still can't get the full path info — as I said, this is deliberately impossible.
bobince
@bobince, your javascript code get the filename without path info in FireFox. But I want to get the filename with path info.
Mike108
You. Cannot. Get. The. Path. Info.
bobince
A: 

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?

geowa4
I want to use the path, so I can upload the file to the same folder automatically.
Mike108
Note that using user-submitted file and directory names is extremely dangerous. Vetting pathnames to make them safe is much more difficult than you might think; much better to use your own filenames internally where you can.
bobince
I use this to upload a file only for myself. So safety problem is not a problem.
Mike108
A: 

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.

bandi
A: 

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.

Peter