Hi Isomorphic,
Version: SmartGWTEE 4.1
We had a situation using com.isomorphic.servlet.ISCFileItem where we needed to get the short filename, without path on the server side.
Unfortunately, IE 8 sends the whole path such that ISCFileItem.getFileName() returns something like c:\path\to\myfile.xls. If the server is UNIX/Linux then it is hard to know what to do since this is a valid UNIX file name. Developing locally on Windows we could create a File in Java to get the short file name or path. FireFox sends myfile.xls to the server so only IE is the problem.
Our use case was that we have files to upload where the filename X23C.txt for example has data inside X23C and we need to know that so we can delete data on load rather than peek inside the file or do something less than optimal.
We tried ISCFileItem.getShortFileName() and it returns null.
This next code shows a form with a file item - not totally code complete. Basically we get the short file name and pass it as a parameter of the request. Ideally the FileItem field would capture the short file name.
This next code is the utility we created to get the shortFileName using core GWT not SmartGWT. Probably a better way but...
Then we have a little helper class on the server side to deal with the ISCFileItem.
We get the shortFileName from the request as shown above. Ideally, SC would pass the shortFileName across and make it available in the ISCFileItem.getShortFileName() method.
Thanks,
Eric
Version: SmartGWTEE 4.1
We had a situation using com.isomorphic.servlet.ISCFileItem where we needed to get the short filename, without path on the server side.
Unfortunately, IE 8 sends the whole path such that ISCFileItem.getFileName() returns something like c:\path\to\myfile.xls. If the server is UNIX/Linux then it is hard to know what to do since this is a valid UNIX file name. Developing locally on Windows we could create a File in Java to get the short file name or path. FireFox sends myfile.xls to the server so only IE is the problem.
Our use case was that we have files to upload where the filename X23C.txt for example has data inside X23C and we need to know that so we can delete data on load rather than peek inside the file or do something less than optimal.
We tried ISCFileItem.getShortFileName() and it returns null.
This next code shows a form with a file item - not totally code complete. Basically we get the short file name and pass it as a parameter of the request. Ideally the FileItem field would capture the short file name.
Code:
import com.ray.gwt.util.FileUtils; import com.smartgwt.client.widgets.form.fields.FileItem; // most imports omitted public UploadForm extends Form { public UploadForm(String title) { super(title); this.setEncoding(Encoding.MULTIPART); this.setDataSource(DataSource.get("ExcelUpload")); FileItem excelFile = new FileItem("excelFile", "Import " + title); excelFile.setWidth(300); excelFile.setWrapTitle(true); excelFile.setType("binary"); IButton importButton = new ImportButton(); importButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Object file = uploadForm.getValue("excelFile"); if ( file != null) { String shortFileName = FileUtils.getShortFileName((String) file); GWT.log("Short File: " + shortFileName); Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("fileName", "excelFile"); // for field name paramMap.put("shortFileName", shortFileName); // added to FileItemBean DSRequest req = new DSRequest(); req.setOperationId("importSapKsh3"); req.setIgnoreTimeout(true); req.setData(paramMap); form.saveData(new DSCallback() { @Override public void execute(DSResponse response, Object rawData, DSRequest request) { // code omiited } }, req); } } }); toolbarItem.setButtons(importButton, resetButton); this.setFields(excelFile, toolbarItem); } }
Code:
package com.ray.gwt.util; import com.google.gwt.user.client.Window.Navigator; /** * Some useful File methods for GWT. * * @author ekr */ final public class FileUtils { /** So no one can instantiate it */ private FileUtils() {} /** * Return the file name without the extension. * * @param fileName - a filename such as foo.txt * @return - e.g. foo */ public static String getBaseFileName(final String fileName) { final int extensionDot = fileName.lastIndexOf('.'); return extensionDot != -1 ? fileName.substring(0, extensionDot) : fileName; } /** * Get the short filename only handles Win and UNIX for now. * @param fileName - a full filename such as c:\work\foo.txt or /work/foo.txt * @return - e.g. foo.txt */ public static String getShortFileName(String fileName) { String platform = Navigator.getPlatform(); String fileSep = platform.equals("Win32") ? "\\" : "/"; int lastIndex = fileName.lastIndexOf(fileSep); return lastIndex != -1 ? fileName.substring(lastIndex + 1) : fileName; } }
Code:
public class FileItemBean { private static final String FILE_NAME_FIELD = "fileName"; private final String fileFieldName; private final ISCFileItem file; private final DSRequest dsRequest; private final String shortFileName; private final long fileSize; private InputStream is; public FileItemBean(DSRequest dsRequest) { this.dsRequest = dsRequest; this.fileFieldName = DmiUtils.getValue(dsRequest, FILE_NAME_FIELD); // form field this.shortFileName = DmiUtils.getValue(dsRequest, "shortFileName"); // if passed in Request try { this.file = dsRequest.getUploadedFile(fileFieldName); } catch (Exception e) { throw new RuntimeException("Unable to get File: " + fileFieldName + " for form field: " + FILE_NAME_FIELD, e); } this.fileSize = file!=null ? file.getSize() : 0; this.is = file.getInputStream(); } }
Thanks,
Eric
Comment