I have a system in which frontend is in javascript/ajax and the backend is written using REST(jersey).
I want to download a file using my system. I have searched various forums and implemented the REST web method as follows :
@POST
@Produces({"text/csv"})
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("getcsv")
public Response getcsv(
@FormParam("usernamecsv") String userid,
@FormParam("filename") String filename
)
{
final File fobj = new File("c:/" +userid + "/output/" + filename);
try
{
final FileInputStream f = new FileInputStream(fobj);
ContentDisposition cd =
ContentDisposition.type("file").fileName(fobj.toString()).build();
Response response = Response
.ok()
.lastModified(new Date(fobj.lastModified()))
.type("application/octet-stream")
.header("Content-Disposition", cd)
.entity(f)
.build();
return response;
}
catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
Initially I had used the StreamingOutput class and implemented the write method for it. In that method I had returned the string read from the file. But I have found no difference between that and the above implementation. Both return the string inside the file.
In my frontend this is what I have done
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function fun1()
{
$.ajax({
url: '/RestWSGS/jersey/UserAuthentication/getcsv',
async: false,
data: $('#form2').serialize(),
type: 'POST',
cache: false,
contentType: "application/x-www-form-urlencoded",
processData: false,
dataType: "text",
success: function(data)
{
var iframe;
iframe = document.getElementById("hiddenDownloader");
if (iframe === null)
{
var iframe;
iframe = document.getElementById("hiddenDownloader");
if (iframe === null)
{
iframe = document.createElement('iframe');
iframe.id = "hiddenDownloader";
//iframe.style.visibility = 'hidden';
$("#mydiv").append(iframe);
}
iframe.src = "http:\\localhost:8080\\c:\abc@abc.com#26 8 2012 13 5 49/gr1/output/test.csv";
//iframe.src = data;
}
alert('Hi');
}
});
}
$(function()
{
$(document).delegate("#mydiv","click",function(ev)
{
fun1();
});
});
</script>
</head>
<body>
<div id="mydiv" style='position:absolute;width:20px;height:20px;background:black'></div>
<form id="form2" enctype="multipart/form-data" method="post" >
<input id ="usernamecsv" name="usernamecsv" type="hidden" value="abc@abc.com#26 8 2012 13 5 49/gr1"/>
<input id ="filename" name="filename" type="hidden" value="test.csv" />
</form>
</body>
</html>
My problem is that I am getting a file not found response( though the data variable has the content of the file), if I assign the iframe.src with the data variable
If I give the uri of the webservice (which I have given to the ajax call) to the iframe.src, I dont know how I will send the parameters.
I really need to show a download prompt to the user and allow him to save the file to the local filesystem. I dont think my REST is right either as it should show a file object in firebug during POST not the contents of the file!
I can just return the string and populate any textarea/ div with it and then ask the user to copy paste it to his file! but that does not look smooth or elegant!
Please help,
Kavita
EDIT:
Tried changing @Produces and type in Response.type() to “application/csv” as well as “application/something” but it always tries to append the contents of the file returned by the server to the current url and opens a search as file is obviously not found!!!
EDIT:
I tried converting POST to GET in the REST and used the data returned. Then it returns a DOCUMENT but still cannot open the file
After a lot of RnD I think I have found the solution to my problem. This might not be the ideal solution and please let me know if there are any major problems with it.
Basically I tried accessing my file on the server and could only access it from the docroot folder (PATH = C:\glassfish3\glassfish\domains\domain1\docroot). So I realized that I must copy my file from wherever on the server to this location. I did that in the Web service. I read somewhere and was trying to send the file itself from the web service. That was wrong after this change I only sent back a plain text with the path relative to docroot.
Then there was another setback as my file had to be within a folder and the folder name contained a # character. It seems # is not allowed and the iframe just wouldn’t ask me for the downloading!!
Now I have modified my code as follows :
REST:
My clientside code:
Hoping someone finds it useful!!
Thanks for the inputs!
Kavita