0

我有一个上传字段。我只想上传图片。我已经申请了相同的验证。但是如果出现错误,我希望将错误添加到表单上的反馈面板中。

上传面板.html

<form wicket:id="frmProduct" >
        <div wicket:id="feedback"></div>
        <label>Category1 *:</label><br/>
        <input type="text" wicket:id="Category1"><br/>
        <label>Category2 *:</label><br/>
        <input type="text" wicket:id="Category2"><br/>
        <label>ProductName *:</label><br/>
        <input type="text" wicket:id="ProductName"><br/>
        <label>TaxAmount *:</label><br/>
        <input type="text" wicket:id="TaxAmount"><br/>
        <label>UnitPrice *:</label><br/>
        <input type="text" wicket:id="UnitPrice"/><br/>
        <label>Description</label><br/>
        <textarea wicket:id="Description" id ="Description" rows="6" cols="20"></textarea><br/>
        <label>Description</label><br/>
        <input wicket:id="uploadField" size="40" type="file"/><br/>
        <input type="submit" wicket:id="submit" value="Save"/>
</form>

上传面板.java

                    if(uploadField.getFileUpload() != null && uploadField.getFileUpload().getClientFileName() != null){
                    FileUpload upload = uploadField.getFileUpload();
                    String ct = upload.getContentType();

                    if (!imgctypes.containsKey(ct)) {
                        hasError = true;
                    }

                    if(upload.getSize() > maximagesize){
                        hasError = true;
                    }

                    if(hasError == false){
                        System.out.println("######################## Image can be uploaded ################");
                        imageEntry.setContentType(upload.getContentType());
                        imageEntry.setImageName(upload.getClientFileName());
                        imageEntry.setImageSize(upload.getSize());
                        if(imageEntry != null){
                            try {
                                save(imageEntry,upload.getInputStream());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }else{
                        target.appendJavaScript("$().toastmessage('showNoticeToast','Please select a valid image!!')");
                        System.out.println("#################### Error in image uploading ###################");
                    }
                }else{
                    System.out.println("########################### Image not Selected #####################");
                }

任何帮助和建议表示赞赏!提前致谢。

4

1 回答 1

2

FileUploadField是一个表单组件,所以你可以简单地做一个

fileUploadField.error("error with file upload");

PS:代替

if(hasError == false)

做一个

if (!hasError)

它更好:)

于 2012-12-22T10:21:51.540 回答