0

I'm trying to create a javascript api to take a word doc as input from the server side (say A) and post it to another api (say B) that will convert it to pdf. The reason I'm doing this is so that i can use the call to B against any A instead of having to modify each of the A APIs (there are multiple As that give word docs).

This is what I have done so far. The problem is when I'm calling B I'm not able to get the file that i'm sending.

Here's my code.

javascript call to server.

$("#downloadFile").click(function(){
        $.ajax({
            url : "/fileDownload.action",
            success : function(data){
                handleFile(data);
            }
        });
    });
});

function handleFile(inputFile){
    $.ajax({
        url : "/convertFile.action",
        type : "POST",
        data : {inputFile : inputFile },
        cache:false,
        processData:false,
        contentType: "application/msword",
        success : function(data){
            alert("yay?");
        }
    });
}

On my server side (a struts 2.3 action class) for "/convertFile.action", I have a setInputFile(File inputFile) method to set the file from request. However, it is not setting the file.

Now if I use a standard file upload with a form in HTML it sets the file (no javascript though, just plain html and server side code).

Also If I try to construct a form and post without an ajax call, I still get the same result. I tried to use the js in this answer to post the form.

What am I doing wrong? One possibility is that I need to take the input as a string or a stream. But is there anything else that I'm doing wrong/violating/can't do?

Community
  • 1
  • 1
Arvind Sridharan
  • 3,333
  • 3
  • 25
  • 51
  • Do you mean javascript than java? – ncm Oct 10 '13 at 08:07
  • @imsiso i'm not able to understand your comment. can you please elaborate? – Arvind Sridharan Oct 10 '13 at 08:10
  • 1
    why pull the file back to the client just to send it to the server again - strange. Anyway, there are security restrictions not allowing jquery/js to post files – Scary Wombat Oct 10 '13 at 08:11
  • @user2310289 - as explained in my question, I want to use a unified service to convert my docs to pdf. Hence the post back to client. Yes i understand that I can modify my server side code across but i'm trying to see if i can do it in javascript itself so that i can just add this additional call in my clients (and any new client also). – Arvind Sridharan Oct 10 '13 at 08:25
  • @ArvindSridharan - nothing you had tagged your question as a java question but I guessed that you meant javascript than java. (cause they are two different things) – ncm Oct 10 '13 at 08:30
  • @imsiso - no i mean java because I'm using struts2 on top of a java application. – Arvind Sridharan Oct 10 '13 at 09:44

0 Answers0