Hello:
Here's what I'm using:
- SmartGWT 2.3 (latest night build from 11/11/2010)
- GWT 2.1
- All browsers
I'm trying to figure out if there is a method to determine file size BEFORE uploading a file to the server. Basically I'm trying to set a maximum of 2MB for image files.
Here's the class I'm using to upload:
Thanks!
Chris
Here's what I'm using:
- SmartGWT 2.3 (latest night build from 11/11/2010)
- GWT 2.1
- All browsers
I'm trying to figure out if there is a method to determine file size BEFORE uploading a file to the server. Basically I'm trying to set a maximum of 2MB for image files.
Here's the class I'm using to upload:
Code:
package com.mybllc.iPerform.client.components; import com.google.gwt.event.shared.HandlerRegistration; import com.mybllc.myapp.client.Utils; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.Encoding; import com.smartgwt.client.types.FormMethod; import com.smartgwt.client.util.SC; import com.smartgwt.client.widgets.Dialog; import com.smartgwt.client.widgets.IButton; import com.smartgwt.client.widgets.events.ClickEvent; import com.smartgwt.client.widgets.events.ClickHandler; import com.smartgwt.client.widgets.events.HasValueChangedHandlers; import com.smartgwt.client.widgets.events.ValueChangedEvent; import com.smartgwt.client.widgets.events.ValueChangedHandler; import com.smartgwt.client.widgets.form.fields.HiddenItem; import com.smartgwt.client.widgets.form.fields.UploadItem; import com.smartgwt.client.widgets.layout.HLayout; public class UploadDialog extends Dialog implements HasValueChangedHandlers { private DynamicCallbackForm _mainForm; private UploadItem _fileUpload = null; private String _uploadArgument = null, _appName = null, _command = null; private UploadDialog _upd = null; public String _fileUploaded = null; public UploadDialog(String uploadarg, String appname, String command) { _uploadArgument = uploadarg; _appName = appname; _command = command; createUI(); _upd = this; } private void createUI() { setTitle("Upload"); setWidth(315); setHeight(100); HLayout h = new HLayout(); h.setWidth100(); h.setHeight100(); addItem(h); _mainForm = new DynamicCallbackForm("Upload"); _mainForm.setAction(Utils.constants.url()); _mainForm.setEncoding(Encoding.MULTIPART); _mainForm.setMethod(FormMethod.POST); _mainForm.setWidth100(); _mainForm.setHeight100(); _mainForm.addFormHandler(new DynamicFormHandler() { public void onSubmitComplete(DynamicFormSubmitCompleteEvent event) { SC.clearPrompt(); if(event.getResults().startsWith("1000")){ String temp = (String)_fileUpload.getValue(); if(temp.indexOf("/") >= 0) _fileUploaded = temp.substring( temp.lastIndexOf("/") + 1 ); else if(temp.indexOf("//")>= 0) _fileUploaded = temp.substring( temp.lastIndexOf("//")+1 ); else if (temp.indexOf("\\")>= 0) _fileUploaded = temp.substring( temp.lastIndexOf("\\")+1 ); else _fileUploaded = (String)_fileUpload.getValue(); ValueChangedEvent.fire(_upd,null); } } }); _fileUpload = new UploadItem(); _fileUpload.setPrompt("Click the button to select the file to upload"); _fileUpload.setName(_uploadArgument); _fileUpload.setWidth(285); _fileUpload.setShowTitle(false); HiddenItem appField = new HiddenItem(); appField.setName("app"); appField.setValue(_appName); HiddenItem cmdField = new HiddenItem(); cmdField.setName("cmd"); cmdField.setValue(_command); HiddenItem heightField = new HiddenItem(); heightField.setName("height"); heightField.setValue("275"); _mainForm.setItems(appField,cmdField,heightField,_fileUpload); h.addMember(_mainForm); IButton saveButton = new IButton(); saveButton.setTitle("Save"); saveButton.setIcon("save.png"); saveButton.setWidth(75); saveButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { save(); } }); IButton cancelButton = new IButton(); cancelButton.setTitle("Cancel"); cancelButton.setIcon("cancel.png"); cancelButton.setWidth(75); cancelButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { _upd.destroy(); } }); HLayout buttonLayout = new HLayout(); buttonLayout.setAlign(Alignment.RIGHT); buttonLayout.setMembersMargin(10); buttonLayout.setWidth100(); buttonLayout.addMember(saveButton); buttonLayout.addMember(cancelButton); setToolbarButtons(buttonLayout); centerInPage(); setIsModal(true); show(); } private void save() { if(_fileUpload.getValue() == null || ((String)_fileUpload.getValue()).length()==0) { SC.say("You must select a file before uploading"); return; } //Lets limit the size of the file upload if its an image.... String filename = (String)_fileUpload.getValue(); if(filename.indexOf(".jpg") > 0 || filename.indexOf(".png") > 0 || filename.indexOf(".gif") > 0){ //Lets check file size.... } SC.showPrompt("<center>Uploading...</center><br><img src=\"images/ajax-loader.gif\"></img>"); _mainForm.submitForm(); } public HandlerRegistration addValueChangedHandler( ValueChangedHandler handler) { return doAddHandler(handler, ValueChangedEvent.getType()); } }
Chris
Comment