-1

I have been trying to load a file trough a form in HTML and send it to Java in order to process it. I made a JavaScript function so i can pass the file path but it won't work because it will send only the file's name and extension so Java will just get just a String to process and throw a NullPointerEception.

Does anyone have any idea how i can solve this problem?

PS: I'm sorry for the noobie question but i don't know JS.

home
  • 12,468
  • 5
  • 46
  • 54
Tatiana
  • 43
  • 2
  • 6
  • 1
    I think it would help if you clarified what JS framework you are using, what Java framework you are using, where it is hosted, what file upload control you are using, etc. Don't be shy of posting source code :-) – Sasha Goldshtein Jan 22 '12 at 09:51
  • Are you using plane servlet or some framework on server side ? – Santosh Jan 22 '12 at 09:59
  • @user1163293 : Your code please... also mention the exceptions if you are getting... also let us know at which code you are facing problem... – Fahim Parkar Jan 22 '12 at 10:36
  • I believe, from HTML you have called Servlet file and trying to read the file... however in Servlet you are getting Null Pointer exception... right?? if yes, in servlet try to print the file path that you are getting... check what you are getting... if it is NULL, check the code you have used for assigning file path as string. – Fahim Parkar Jan 22 '12 at 10:37

3 Answers3

0

i think the file which you forward for java is must read by input output operation in bytes beacuse java is understand only text file or byte array by byte array you can store any image ,pdf etc.

ashish geol
  • 87
  • 2
  • 5
0

"The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data."

http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2

What does enctype='multipart/form-data' mean?

So basically it doesn't really matter how your submit your form on the client, <input type="submit" />, some java script like document.forms["myform"].submit(); or however your js library does it, as long as you have <input type="file" /> in your form on the client and some server side component (like servlet), which could get to the binary file submitted, from the request.

For e really comprehensive example and explanation see this post: How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
Svilen
  • 1,377
  • 1
  • 16
  • 23
0

i have a regular html form like this:

<form action="MultipartServlet" name="form" id="form" method="post" enctype="multipart/form-data">
<td><input type="file" name="upload" id="upload" />
<td><input type="button" value="Check" onclick="FileValidator.check()"/>

i cannot use type="submit" because for an odd reason the application is crushing

the JS code:

check: function() {
    var file = $F("upload");

        new Ajax.Request( 'url', {
            parameters: '...&action=fileValidator&upload=' + file,
            onSuccess: function(response) {
                var result = eval('(' + response.responseText + ')');
                if (result.success) {
                    displayErrorsFromFile();
                } else {
                    alert("Errors! " + response.responseText);
                }
            },
            onFailure: reportError
        })
    }

in the Java code i just try to get the file trough the "upload" parameter and validate the file's imput.

so i guess that the "upload" parameter has to get a bites array of the entire file so it can process it... or somehow the path of the file

Tatiana
  • 43
  • 2
  • 6