Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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!!</p> <p>Now I have modified my code as follows : REST:</p> <pre><code>@POST @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Path("getcsv") public String getcsv( @FormParam("usernamecsv") String userid, @FormParam("filename") String filename ) { System.out.println("1 = " + getClass().getResource("/" +getClass().getName().substring( 0, getClass().getName().indexOf("."))).getPath()); System.out.println("2 = " + getClass().getResource("/" +getClass().getName().substring( 0, getClass().getName().indexOf("."))).getPath() + "../../../../../docroot/" + userid + "/" + filename); final File fobj = new File("c:/" +userid + "/output/" + filename); try { final FileInputStream f = new FileInputStream(fobj); int content; ByteArrayOutputStream b = new ByteArrayOutputStream(); try { while ((content = f.read()) != -1) { //b[j] = 0; // convert to byte b.write(content); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (f != null) f.close(); } catch (IOException ex) { ex.printStackTrace(); } } File f1 = new File( getClass().getResource("/"+getClass().getName().substring( 0, getClass().getName().indexOf("."))).getPath() + "../../../../../docroot/" + userid.substring(0,userid.indexOf("#")) + "/" ); f1.mkdirs(); try { boolean f2 = new File(getClass().getResource("/" +getClass().getName().substring( 0, getClass().getName().indexOf("."))).getPath() + "../../../../../docroot/" + userid.substring(0,userid.indexOf("#")) + "/" + filename).createNewFile(); System.out.println(f2); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } FileOutputStream fout = new FileOutputStream(new File(getClass().getResource("/" +getClass().getName().substring( 0, getClass().getName().indexOf("."))).getPath() + "../../../../../docroot/" + userid.substring(0,userid.indexOf("#")) + "/" + filename)); try { fout.write(b.toByteArray()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); try { fout.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } try { fout.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return "/" + userid.substring(0,userid.indexOf("#")) + "/" + filename; </code></pre> <p>My clientside code:</p> <pre><code>&lt;!DOCTYPE html &gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;script type="text/javascript" src="jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;script type="text/javascript"&gt; 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 = data; alert('Hi'); } }); } $(function() { $(document).delegate("#hiddenDownloader","onload",function(ev) { alert('in onload'); }); $(document).delegate("#mydiv","click",function(ev) { fun1(); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a id ='myhref' href=""&gt;&lt;/a&gt; &lt;div id="mydiv" style='position:absolute;width:20px;height:20px;background:black'&gt;&lt;/div&gt; &lt;form id="form2" enctype="multipart/form-data" method="post" &gt; &lt;input id ="usernamecsv" name="usernamecsv" type="hidden" value="abc@abc.com#26 8 2012 13 5 49/gr1"/&gt; &lt;input id ="filename" name="filename" type="hidden" value="test.csv" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Hoping someone finds it useful!! Thanks for the inputs!</p> <p>Kavita</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload