PDA

View Full Version : FileUpload fakepath



Jenya
24 Aug 2010, 9:52 AM
Using FileUpload on selection of any file, the path in the field is written always like:

C:\fakepath\[my file real name].

Here is a test case:



RootPanel rp = RootPanel.get("main");

FormPanel form = new FormPanel();
form.setFieldWidth(200);
form.setLabelWidth(150);
form.setFrame(true);

final FileUploadField fileUpload = new FileUploadField();
fileUpload.setFieldLabel("File");
form.add(fileUpload);

form.addButton(new Button ("click me", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
MessageBox.alert("", fileUpload.getValue(), null);
}
}));

form.layout();

raghumitra
24 Sep 2010, 6:34 AM
This is a security feature in IE8 to prevent exposing of system structure when you upload a file. This behavior can be controlled in the Security tab of Internet Options
http://msdn.microsoft.com/en-us/library/ms535128(VS.85).aspx

cloudmu
31 May 2011, 5:49 AM
Using FileUpload on selection of any file, the path in the field is written always like:

C:\fakepath\[my file real name].

Here is a test case:



RootPanel rp = RootPanel.get("main");

FormPanel form = new FormPanel();
form.setFieldWidth(200);
form.setLabelWidth(150);
form.setFrame(true);

final FileUploadField fileUpload = new FileUploadField();
fileUpload.setFieldLabel("File");
form.add(fileUpload);

form.addButton(new Button ("click me", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
MessageBox.alert("", fileUpload.getValue(), null);
}
}));

form.layout();


You can override the onChange method of the FileUploadField, to get rid of the "fakepath" prefix shown on the UI. Note this wouldn't affect the real path of the file to be uploaded. Works fine in the Chrome 11 and IE 9.

final FileUploadField fileUpload= new FileUploadField(){
// this is to resolve the "fakepath" issue
@Override
protected void onChange(ComponentEvent ce) {
final String fullPath = getFileInput().getValue();
final int lastIndex = fullPath.lastIndexOf('\\');
final String fileName = fullPath.substring(lastIndex + 1);
setValue(fileName);
}
};

By the way, I couldn't reply to Jenya's original post at
http://www.sencha.com/forum/showthread.php?107981-FileUpload-fakepath
The forum prompts that I don't have sufficient privilege.

WesleyMoy
15 Dec 2011, 7:10 AM
cloudmu's solution works perfectly and is the idiomatic approach. By hooking into onChange, changes that have been made to the actual (hidden) file upload field have not yet been reflected in the visible text field. It's worth noting that, on non-Windows systems, it could be possible to have a file with a backslash in the name, which will cause this approach to show an incorrect filename. Consider instead case-insensitively stripping "C:\fakepath\" from the start of the string.

I'll add a link to this thread from Jenya's thread in Ext GWT Premium Help.