Stack Exchange
Not getting html response from servelet through ajax
I have made ckeditor plugin to convert doc file to html, the file is uploaded via servlet witha ajax and converted to html. But I am not getting desired html response from servlet. I am getting the same jsp page in result.
Here is Servlet code.
        protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException { 
    // HttpSession
            response.setContentType("text/html");                                                                           // session;
            if (!ServletFileUpload.isMultipartContent(request)) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN,
                        "only multipart requests are allowed");
                return;
            }
            WebappContext webappContext = WebappContext.get(getServletContext());
            ServletFileUpload fileUpload = webappContext.getFileUpload();

            OfficeDocumentConverter converter = webappContext
                    .getDocumentConverter();
            String outputExtension = FilenameUtils.getExtension(request
                    .getRequestURI());
            FileItem uploadedFile;
            try {
                uploadedFile = getUploadedFile(fileUpload, request);
                // FileHandler.checkForValidExtn(uploadedFile);
            } catch (FileUploadException fileUploadException) {
                throw new ServletException(fileUploadException);
            }
            if (uploadedFile == null) {
                throw new NullPointerException("uploaded file is null");
            }
            String inputExtension = FilenameUtils.getExtension(uploadedFile
                    .getName());
            String baseName = FilenameUtils.getBaseName(uploadedFile.getName());
            // File fileFolder=new File(getServletContext().getRealPath("/")
            // +"images");
            // fileFolder.mkdir();
            // File inputFile=new File(fileFolder+"\\"+ baseName + "." +
            // inputExtension);
            File inputFile = new File(getServletContext().getRealPath("/")
                    + baseName + "." + inputExtension);

            writeUploadedFile(uploadedFile, inputFile);
            File outputFile = new File(getServletContext().getRealPath("/")
                    + baseName + "." + "html");
            // File outputFile=new File(fileFolder+"\\"+ baseName + "." + "html");
            // outputFile.mkdirs();
            System.out.println("input file path:" + inputFile.getAbsolutePath());
            System.out.println("outPut file path:" + outputFile.getAbsolutePath());
            try {
                // DocumentFormat outputFormat = converter.getFormatRegistry()
                // .getFormatByExtension(outputExtension);
                long startTime = System.currentTimeMillis();
                converter.convert(inputFile, outputFile);
                long conversionTime = System.currentTimeMillis() - startTime;
                logger.info(String.format(
                        "successful conversion: %s [%db] to %s in %dms",
                        inputExtension, inputFile.length(), outputExtension,
                        conversionTime));
            } catch (Exception exception) {
                logger.severe(String.format(
                        "failed conversion: %s [%db] to %s; %s; input file: %s",
                        inputExtension, inputFile.length(), outputExtension,
                        exception, inputFile.getName()));
                throw new ServletException("conversion failed", exception);
            } finally {
                 inputFile.delete();
            }
            PrintWriter out = response.getWriter();
            out.println("<b>File Sent to server</b>");
            out.close();
}

Here is code javascript code for sendi file to server.
CKEDITOR.dialog.add( 'uploadDoc', function( editor ) 
{    var dialougeDefination = 
        {   title:          editor.lang.uploadDoc.labelName,
            resizable:      CKEDITOR.DIALOG_RESIZE_BOTH,
            minWidth:       250,
            minHeight:      50,
            onOk:       function()
                {   if(document.getElementById("flFileBrowser").value == "")
                        {   alert("No file selected");
                            //return;
                        }
                        else
                        {
                            $.ajax({

                                beforeSend: function(){
                                    {    $("#frmFileUpload").ajaxSubmit();
                                    $.blockUI();
                                    }
                                },

                                success: function(result)
                                {   alert(result);

                                    $.unblockUI();
                                }

                            });


so there is plenty of code there to debug. Do you know if the problem is on the server or the client.

Looks like the browser you're using to test this doesn't support file upload by ajax (more info here. It would be better using/adapting a plugin for this like blueimp jquery file upload plugin

Related: http://stackoverflow.com/q/166221/1065197

The file is getting uploaded and getting converted to html.
The problem is that servlet is not sending response in result string.
I am getting response as the same jsp page where I placed, ckeditor.

is ckeditor in an iframe?

No it is attached to textarea, there's no iframe

ah so you want result.responseText

yes I want the response html string from Servelet

Yes, so alert(result.responseText)

The response is actually in the responseText field, so your code should be 
alert(result.responseText);


i have written alert(result); result is coming as pure string there are no properties associated.

success: function(result) - this result has a responseText field.  See http://stackoverflow.com/questions/7598821/return-responsetext-from-jquery-get

result.responseText is undefine, might be
beforeSend: function(){
{    $("#frmFileUpload").ajaxSubmit();
$.blockUI();
}
}
is creating an issue. in beforeSend callback i am uploading file
