Open Bug 476633 Opened 12 years ago Updated 11 years ago

nsIFile retrieved from drag and drop improperly urlencodes file name when it's already encoded


(Core :: DOM: Drag & Drop, defect)

Not set





(Reporter: EliteBadger, Unassigned)


User-Agent:       Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/2008121300 SUSE/3.0.5-1.1 Firefox/3.0.5
Build Identifier: Mozilla XULRunner - 2008121300

I'm writing a XUL application. When I drag a file onto my application and it enters my drop handler, the file name is url-encoded in some bizarro-format.
* Consecutive spaces will be encoded. so foo__bar where each _ is a space becomes foo%20_bar
* Percent signs will be escaped, so foo%bar becomes foo%%bar
* The bug happened when I took an already-encoded file like foo%20bar and it became foo%%2520bar (so it both escapes the percent sign and also encodes it!)

The resulting nsIFile is broken, because it thinks that the underlying file doesn't exist.

Reproducible: Always

Steps to Reproduce:
* Get a file from drag and drop
function onFileDrop(event) {
	var session = drag_svc.getCurrentSession();
	for (var i = 0; i < session.numDropItems; ++i) {
		var xfer = Components.classes[';1'].createInstance(Components.interfaces.nsITransferable);
		session.getData(xfer, i);

		var len = {};
		var data = {};
		try {
			xfer.getTransferData('application/x-moz-file', data, len);
			var file = data.value.QueryInterface(Components.interfaces.nsIFile);
		} catch(e) {
		if (!file.exists()) {
			err_print("How did you give me a file that doesn't exist?");

* Open Konqueror and create a file named foo%20bar
* Drag the file onto the application whose drop handler is the above code
* The file.exists property will be false and the error will print

Actual Results:  
"How did you give me a file that doesn't exist?

Expected Results:  
Function exits normally.

The whole URL encoding thing is such a pain to work around, especially the handling of percent signs. Why does it have to be that way?
Component: XPConnect → Drag and Drop
QA Contact: xpconnect → drag-drop
The other buggy thing about this bug is that if I have a file "foo%bar", it becomes "foo%%bar". Fine, but if I then set file.nodeName to "foo%bar" I get a file not found error. 

Is there any workaround for this behavior? I don't want my users to see filenames that don't match the file on disk, but it seems really broken to have to keep a display name variable around that translates the Mozilla Wrong Filename (tm) to the one they would expect.
The double-percent thing is a konqueror bug, not a mozilla bug! My bad. Apparently if you create a new file from konqueror and name it foo%bar it becomes foo%%bar. Creating one from the command line via `touch foo%bar' works the way it should.

It's still a little wacko that getting an nsIFile from foo%bar results in an nsIFile object with leafName foo%2520bar, which then fails when you call file.exists().
You need to log in before you can comment on or make changes to this bug.