Rectangle 27 1

Unfortunately, that's not possible with HTTP. You can send only one response back per request. You cannot merge the response containing the PDF file and the ajax response into one response. Since this is a HTTP restriction, JSF can't do any much for you. Also, downloading a file using Ajax is not possible at all since JavaScript can't force the browser to pop a Save As dialogue nor have any access to local disk file system due to security restrictions.

A workaround would be to fire two HTTP requests on a single button click where the second request returns Content-Disposition: attachment so that the response of the other request keeps untouched. You can achieve this by adding an onclick to the command button.

<h:commandButton onclick="window.location='/context/pdfservlet/filename.pdf'">

and create a PDF servlet which roughly look like this FileServlet example. As you see, it's not possible to invoke a JSF action by this. You have to refactor the PDF download method to a HttpServlet class which does the job in doGet() method. For any necessary communication between the JSF managed bean and the servlet, you could use the session scope or pass the desired information (just the PDF file identifier?) by request path or parameter.

Thanks for the explanation. I'll just keep using the two buttons.

There's actually a way. See update.

input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
new BufferedInputStream(new ByteArrayInputStream(output_b.toByteArray()), DEFAULT_BUFFER_SIZE);
output_b
ByteArrayInputStream
InputStream
onclick="servlet?param1=#{bean.param1}&amp;param2=#{bean.param2}"
String param1 = request.getParameter("param1")

I'm using an external library which allows me to turn Java into xml, and I'm outputting them into a ByteArrayOutputStream output_b. I will have to think about this HttpServlet more. Thanks.

java - h:commandButton multiple actions: download file and render ajax...

java ajax jsf jsf-2 facelets
Rectangle 27 2

If the problem is the batch file is being displayed in the browser you need to set Content-Type and Content-Disposition in the HTTP header so the user is prompted to Save (or Run) the file rather than have the browser display it.

You won't be able to run the file without an OK from the user but this shouldn't be a problem.

javascript - How can I run a program or batch file on the client side?...

javascript batch-file executable client-side
Rectangle 27 1

You can read the PDF as a bytestream from the database and save it to the http response stream. If you have set the content type correctly to application/pdf, then the browser will load the document in the PDF plugin.

Update (14/Oct/2011): You need to write the bytestream to the Response.OutputStream object. How you create and write the byte stream is dependent on how you have stored in the database and how you are retrieving it. The following code snippet is from an article we have on our website - Generate PDF Forms In ASP.NET Using PDFOne .NET v3.

The doc object is from our component. You need not use that. This code snippet is only for your understanding. For your requirement, you may have to something like bytestream.save(Response.OutputStream) I guess. BTW, this code is for ordinary ASP.NET, not MVC.

Coul you direct me to a resource that woudl explain how to do al three of these steps?

how do i save it to the http response? How do I display it in the view once it is in the http response? How do I get it from the database in byte stream format?

In ordinary ASP.NET, you write the bytestream to the Response.OutputStream after setting the content type to application/pdf. The response code can work in a separate page or an IFRAME. The PDF is usually stored as a blob object in a database. You need to retrieve that blob first and create a bytestream out of that. It is also dependent on how you have stored the PDF in the database. If have written the code store it, then you will easily know how to extract it.

c# - Displaying pdf files in a web page from a sql database directly w...

c# sql asp.net-mvc-3 pdf embed
Rectangle 27 1

(One note: the notion of "forced as an attachment" doesn't make sense in the context of an FTP server. FTP servers just serve bits; it's up to your FTP client to decide what to do with them. This is in contrast to HTTP servers, which serve those bits but also serve metadata about the bits (e.g., the HTTP headers); it's that metadata that instructs your client what to do (display vs. force a download/save action). When you use your browser as an FTP client, the browser makes a decision about how to handle the response to an FTP GET request, and that process is entirely separate from how it makes a decision about how to handle the response to an HTTP GET request.)

Have you tried your code with hardcoded values first, to make sure that the issue isn't that you're passing in unexpected or unanticipated value data in the $network and $_FTP variables? In other words, test this, substituting a known-good username, password, hostname, and filename, and making sure you have rights to write a file to the current working directory:

$curl = curl_init();
$url = "ftp://hostname/filename.gz";
$file = fopen("filename.gz", "w");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_exec($curl);
curl_close($curl);
fclose($file);

In any event, you can also add the following to get debugging information sent back to your browser:

curl_setopt($curl, CURLOPT_VERBOSE, 1);

If you add that to your code, what debugging information do you get back?

I get nothing... I know that the values used are ok (I checked them with echo to see the url and parameters passed to curl)

ftp - Download a file that is forced as attachment with PHP - Stack Ov...

php ftp curl download
Rectangle 27 1

You can't call the JFileChooser from the servlet because the servlet runs on the server, not on the client; all of your Java code is executed on the server. If you want to save the file on the server, you need to already know the path you want to write to.

If you want to prompt the user's browser to save the file, use the content-disposition header: Uses of content-disposition in an HTTP response header

It looks like this will help as well if anyone else stumbles on this question: stackoverflow.com/questions/899858/

java - How to use a save file dialog from a servlet? - Stack Overflow

java swing jsp servlets tomcat5.5
Rectangle 27 1

You can't call the JFileChooser from the servlet because the servlet runs on the server, not on the client; all of your Java code is executed on the server. If you want to save the file on the server, you need to already know the path you want to write to.

If you want to prompt the user's browser to save the file, use the content-disposition header: Uses of content-disposition in an HTTP response header

It looks like this will help as well if anyone else stumbles on this question: stackoverflow.com/questions/899858/

java - How to use a save file dialog from a servlet? - Stack Overflow

java swing jsp servlets tomcat5.5
Rectangle 27 0

Just write the PDF to the HTTP response instead of to a local file.

PdfWriter.getInstance(document, new FileOutputStream("D:/Files/ABC.pdf"));
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"ABC.pdf\"");
PdfWriter.getInstance(document, response.getOutputStream());

// ... (your PDF building code here)

context.responseComplete();

and invoke it in action method associated with download button. The Content-Disposition header with value of attachment will force a Save As dialogue (unless the browser is configured to automatically open PDF files in Adobe Reader!).

Update: as per your update, you're invoking the download by ajax.

<a4j:commandButton value="Print" styleClass="button" action="#{declarationBB.convertToPdf}"></a4j:commandButton>

This won't work. You can't download files by ajax. Ajax is executed by JavaScript, but it has due to obvious security reasons no facilities to programmatically force a Save As dialogue with asynchronously obtained/generated content in some variable. Make it a normal request instead. Use <h:commandButton> instead.

It is redirecting to other page and displays somthing in encrypted form (=('VKzq/"]! 1v&Xw~s5:;?cueAY)

That's not encrypted. That's just the binary PDF content presented as text. Apparently you didn't set the proper content type header so the browser didn't understood what to do with it.

I said content type, not content disposition. It's the response.setContentType("application/pdf"); line as in my answer. By the way, how exactly is the action invoked? Another possible cause would be that it's invoked in an inappropriate manner. Do you have any servlet filters on the request which manipulate the response headers? That would be another possible cause. It would also be helpful if you post which response headers the browser ultimately got.

download - Save dialog box in jsf - Stack Overflow

jsf download jsf-1.2
Rectangle 27 0

You have to add Expires to your http response headers. This will make the browser in the client side to save the component of the page until that date. If you wonder how to set the headers while sending a response, you have to know to configure your server either by using a back-end programming language, or a third party.

<meta http-equiv="Cache-control" content="public">

It did not work

Cache-Control: no-cache
Cache-Control: max-age=2592000

Enable Caching of Javascript, CSS files which are included in a JSP fi...

javascript jsp browser browser-cache cache-control
Rectangle 27 0

After following the above answer and this tutorial

This worked for me to save a Google Places image to an ImageField:

First, prepare the URL to which HTTP Response is an image:

city_image_url = ('https://maps.googleapis.com/maps/api/place/photo'
        '?maxwidth=%s'
        '&photoreference=%s'
        '&key=%s') % (maxwidth, city_image_ref, GoogleKey)

Second, retrieve that actual image and save it to a variable:

retrieved_image = requests.get(city_image_url)

Third, create a file locally and write the remote image's content to it:

with open(city_names[i] + '.jpg', 'wb') as f:
                f.write(retrieved_image.content)

Fourth, open the file with the image inside it and convert it to a Django File:

reopen = open(city_names[i] + '.jpg', 'rb')
            django_file = File(reopen)

Fifth, create a model object and save the django file inside the ImageField like this:

created_city = City.objects.get_or_create(city_id=city_id)[0]
#save it to the ImageField -> args are: 1. the name of the file that is to be saved in MEDIA_ROOT path 2. The Django file itself 3. save instance
created_city.city_image.save(city_names[i] + '.jpg', django_file, save=True)
created_city.save()
from django.core.files import File
import requests

python - How to save an image from Google Places API to an ImageField ...

python django django-models google-places-api imagefield