Hi,

I have a datatable editor and I'm using ajaxFileUpload to upload an image.

I can edit an item in datatable and for example change it's picture -
upload a new one. Now I've stumbled upon an issue: I can't seem to
determine (from JavaScript) if there was an error uploading file on the
server side.

I'm trying to return JSON object with error message in the catch block in
Java, but if I do that I get "Return type
org.apache.tapestry5.json.JSONObject can not be handled." error.

And now my question is - how come this is not an AJAX request?
Should I put the ajaxFileUpload code to some other location?


This is my JavaScript code:

editor.on('onPreSubmit', function ( e, json ) {
    e.preventDefault();

    $.ajaxFileUpload({
         url:editorOptions.imageUploadUrl + '?id=' + json.id +
'&field=imageFile',
         secureuri:false,
         async: false,
         fileElementId:'imageFile',
         dataType:'xml',
         success:function (data, status) {
             var rowId = $('#id', eval(data)).html();
             var imageFilename = $('#imageFilename', eval(data)).html();
             var rowIndex = oTable.fnGetPosition(oTable.$('tr#' +
rowId)[0]);
             var imageFilenameIndex = 1;
             oTable.fnUpdate(imageFilename, rowIndex, imageFilenameIndex);
         },
         error: function (data, status) {
             alert("Error while uploading image: " + $(eval(data)).text());
         }
         })
    } );



and Java:


    @OnEvent(component = DATATABLE_ID, value = EVENT_UPLOAD_IMAGE)
    public Object itemImageUpload(@RequestParameter("id") String id,
RequestParameter("field") String field) {
        UploadedFile file = this.multiPartDecoder.getFileUpload(field);

        ModuleItem item = this.moduleItemDAO.findById(Long.parseLong(id));

        logger.debug("IS AJAX: " + request.isXHR());    // this is false!

        try {
            if (file != null && file.getSize() > 0) {
            String webDavUrl =
symbolSource.valueForSymbol(WebDavManager.PROPERTY_WEBDAV_URL);
            String fileName = webDavUrl + "/" +
this.webDavManager.putFile(WebDavFileType.BACKGROUNDS, file);

             item.setImageFilename(fileName);
                this.moduleItemDAO.save(item);
            }
            return new TextStreamResponse("text/html", "<html><div
id=\"id\">" + id + "</div><div id=\"imageFilename\">" +
item.getImageFilename() + "</div></html>");
        } catch (Exception e) {
        // TODO: return JSON error message to display on frontend
     return null;
        }
    }


Kind regards,
Lidija

Reply via email to