File download in REST uing jersey and RESTclient in firefox not showing download pop up - rest

I'm writing a code to download files using ReST API. my code is working fine and I am getting a response back.
But i am getting the file content as response body in browser rather than asking for a file download
below is the sample code I am using.. please help me to figure out what i am doing wrong.
I tried all the codes posted in SO that matched with my requirement but still i could not get the pop up .
#Path("/downloadfiles")
public class DownloadFiles {
#GET
#Produces("text/plain")
public Response getFiles( ) {
File file = new File("/opt/test/sample.csv");
ResponseBuilder response = Response.ok((Object) file).header("Content-Disposition",
"attachment; filename=sample.csv");
return response.build();
}
}
Thanks.

Hello try to make it produce "application/octet-stream" instead of text/plain
The kind of content you specify is the reason you only see text
hope it helps

Try #Produces("test/csv") and if that alone doesn't work try also setting the entity on the ResponseBuilder to the String contents of your file. That works for me, anyway.
BTW, in my browser I don't get a popup, it just starts downloading the CSV file immediately.

Related

Downloading a PDF from an HTTP POST call

Here is what I'm trying to accomplish (IE 9+, Chrome, FF, Safari) without the use of JQuery:
Make an http POST call to my API endpoint with some data
Server dynamically generates a PDF and returns the PDF as a binary attachment
Browser does default download behavior and downloads the PDF without refreshing the page
Basically I want to get the behavior similar to <a href="test.pdf"> but for a dynamically generated PDF after making a POST call instead of a GET call.
I've tried lots of different things, but they either didn't work cross browser (such as using $window.open() with a blob URL), were blocked by popup blockers (any $window call outside of the click scope), or didn't cause the PDF to be automatically downloaded (any $http POST solution).
I finally found one solution that seems to work which creates a form using javascript and submits it.
var form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', myurl);
var params = {foo: 'bar'};
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', key);
hiddenField.setAttribute('value', params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
This successfully accomplishes the 3 steps above, but now I've run into a new problem. There is no way to determine when the PDF file has been successfully downloaded. This is preventing me from removing the form and from displaying a friendly 'Please wait...' message to the user. There is also the additional problem that submitting the form cancels any outstanding ajax requests as well which isn't optimal.
I have full control over both the server and the client, so what's the best way to fix this? I don't want to have to save the PDF on the server so passing back a url and doing a second GET request from the client won't work in this case. Thanks!
You can make an server response behave as a download by applying some HTTP headers:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="SOME_NAME.pdf"
Content-Transfer-Encoding: binary
If you're initiating the download through JS only (instead of having the user click a download link), then check out this question for some caveats.
Update: Syntax for POST
Better Update: Form solution with iframe target
You can detect that your server-side script has finished (and subsequently, that the download is ready to begin) by having the form target an iframe. I believe this should also fix the issue of cancelling outstanding Ajax calls, but I'm not certain. Here is the code to do it (just stick this into your code example after the for loop and before document.body.appendChild(form);):
var frame = document.createElement('iframe');
frame.setAttribute('id', 'pdfFrame');
frame.onload = function(){
document.body.removeChild(form);
document.body.removeChild(frame);
alert('Download ready!');
}
document.body.appendChild(frame);
form.setAttribute('target', 'pdfFrame');
You can replace my alert with your code to remove the 'Please wait...'.

In phonegap 1.4.1 in iOS6 unable to attach xml file from to mail client

to get help , I followed this post http://www.thoughtsonmobile.com/category/development/crossplattform-app-development
My objective is to use PhoneGap 1.4.1 plugin in iOS to write to an XML file and send that xml file via attachment in mail client
I am able to create an empty XML file in the path /var/mobile/Applications/D7558E05-1345-443E-A2E6-.../Document/readme.xml which I verified under Iexplorer to be under the Documents folder
I followed the steps in the mentioned site, exactly, but there is one confusion on how to pass the path to the Phonegap.exec command.. Should I pass the complete path "/var/mobile/Applications/D7558E05-1345-443E-A2E6-.../Document/readme.xml"
should I pass as follows
window.plugins.emailComposer.showEmailComposerWithCB(null, "subject", "body", "hello#gmail.com","ccsupport#test.com","bccsupport#test.com", false,XMLFilePath);
where
var XMLFilePath = window.rootFS.fullPath + "/" + "readme.xml";
or should it be just XMLFilePath = window.rootFS.fullPath
As mentioned in the site, in the EmailComposer.js I added
if(attachment)
args.attachment = attachment;
and checked, by alerting if the args.attachment is getting the path, its coming as /var/mobile/Applications/D7558E05-1345-443E-A2E6-.../Document/readme.xml
I added snippet as per the site, "NSString* attachmentPath = [options valueForKey...."to the EmailComposer.m code from "https://github.com/phonegap/phonegap-plugins/blob/master/iOS/EmailComposer/EmailComposer.m" which I am using
When I run the code I am NOT getting the attachment, am oly getting the subject, to and body
Please help
It would be very useful if you could upload a sample, complete xcode project for mail attachment.

POST submit fails and redirects

This is something weird. I'm letting users submit a URL using a POST form. This form and everything that has anything to do with it is in a subfolder in my main public_html. The resulting PHP, which lies on the same page as the form, does this:
if (filter_var($_POST['input-addr'], FILTER_VALIDATE_URL)) {
$_SESSION['scy-prox-url'] = $_POST['input-addr'];
} else {
$_SESSION['scy-prox-url'] = 'false';
}
Below that, it gets the contents of the requested URL file and does a huge amount of editing to the HTML.
If I enter something simple, like http://smashingmagazine.com/, it seems to fail and redirect me to a 404 not found page.
I created a GET client that does the same thing as above but using $_GET which works.
Any ideas?
As it works for me here: http://codepad.org/zyXTCJDZ
I suspect it may be something in the code below what you've pasted.

Get Downloads from github via php api

I'm trying to get all downloads from an repo on github with help of https://github.com/ornicar/php-github-api
There is no download Object in this project, so i've created my own:
class Github_Api_Download extends Github_Api{
public function getDownloads($repo, $username){
$string = 'repos/'.urlencode($username).'/'.urlencode($repo).'/downloads';
$response = $this->get($string);
die(print_r($response));
return $response;
}
}
When i run this, i'm getting only
Server Error
HTTP 401: Unauthorized
it should be possible to get this without authentication too, because if i use the url in the browser, it's working
my authentication is working, because all the actions (e.g. create new repo,etc..) are working fine
Anybody can help me?:)
I was looking for this earlier, so I wrote my own little function and posted it on my blog. Hope this helps.
Check this out

GWT:How to generate pdf save/open window?

I am using GWT2.4 version in my application.In this I application I have created Form using GWT control (like textbox,textaera).
I have also created preview of form.In that preview I have button of pdf generation.
Now I want to create behavior to deal with pdf link same as browsers(Mozilla/chrome).
For example in Mozilla on click of pdf link it asks for either save or open in a pop up window.
While debugging I found a jar name iText which can be used to create pdf, I want to implement browsers behavior in this also.
Please help me out.
Thanks in advance.
Read file contents into byte array.
Then do request for servlet or service, for eg. this way:
Window.Location.replace("rest/downloadPdf");
That Service should return Response with the right content type:
#Path("downloadPdf")
#GET
#Produces({"application/pdf"})
#Consumes(MediaType.TEXT_PLAIN)
public Response downloadPdf() throws Exception {
byte[] bytes = getYourPDFContents();
return Response
.ok(bytes, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"yourFile.pdf\"")
.build();
}
Browser will then show save as dialog.
That's example of Service, you must include Jersey library into your project to be able to use method like I've wrote above .