0

I am uploading a .war file in jsp as follows,

<input type="file" name="file1" id="file1" size="50">

example: C:\1234\OnlineBookStore.war

and uploading the form using ajax as follows,

var fileValue = document.getElementById("file1").value; 
url=url+"&fileName="+fileValue;
loadPage('downloaddiv', url);

when i get the filename in java i am just getting the name of the file(OnlineBookStore.war

) without its contents,

File uploadedFile = new File(request.getParameter("fileName"));

how to get the file with contents form request?

Thanks

user1321824
  • 445
  • 3
  • 22
  • 41

2 Answers2

1

You need to use the File API if you want to read the data from files to upload via XMLHttpRequest.

There is a guide to Using files from web applications on MDN.

Note that this requires a browser that supports this bleeding edge feature.

The classic way to upload files via Ajax is to submit a form to a hidden iframe.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Maybe you should checkout this question: How to get the file path from HTML input form in Firefox 3

There is little to no reason why server should have to know full file path. If you want to upload a file, you'll need to use an appropriate library like Apache Commons FileUpload and transfer the file using.

<form action="upload-script-url" method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit">
</form>

Apache Commons FileUpload will then accept and transform encoded file into a usable form.

Otherwise you'll need to use JavaScript to get that path.

Community
  • 1
  • 1
Sathish
  • 4,403
  • 7
  • 31
  • 53