I am generating a xml file using JAXB but at present file is generated at specified location,How can i use a browse button to specify the location of folder to save the generated file.
Have tried with input type="file" of HTML but it is useful for uploading the file.Want it to do from rich faces only.
Just write it directly to the HTTP response along with a Content-Disposition header with a value of attachment. This will force the browser to pop a Save As dialogue.
So, essentially all you need to do is to marshal the XML tree straight to the output stream of the HTTP response instead of the output stream of the file after having set the proper headers.
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
// ...
ec.responseReset(); // Make sure the response is clean and crisp.
ec.setResponseContentType("text/xml"); // Tell browser which application to associate with obtained response.
ec.setResponseCharacterEncoding("UTF-8"); // Tell browser how to decode the characters in obtanied response.
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // Tell browser to pop "Save As" dialogue to save obtained response on disk.
marshaller.marshal(model, ec.getResponseOutputStream()); // Look ma, just marshal JAXB model straight to the response body!
fc.responseComplete(); // Tell JSF that we've already handled the response ourselves so that it doesn't need to navigate.
Note: downloading a file via ajax is not possible. Remember to turn off the ajax feature of the RichFaces/Ajax4jsf command component invoking this method, if any.
See also:
How to provide a file download from a JSF backing bean?
Related
I am trying to modify "Content-Disposition" value in "multipart/form-data" POST request with Fiddler that is automatically generated by webpage's javascript with attached image on file upload.
However, I cannot find a way to do it: when I try to simply replace the "Content-Disposition" Fiddler seems to convert entire content to string, replace the Content-Disposition string and convert back to bytes, which seems to break the attached image.
Several examples of code I have tried to use:
oSession.utilReplaceInRequest("string1","string2")
var strBody=oSession.GetRequestBodyAsString();
strBody=strBody.replace("string1","string2");
oSession.utilSetRequestBody(strBody);
While these successfully replace string1 with string2, the attached image is no longer valid.
What would be a way to do it?
Let say we have some file at http://somedomain.com/somedir/file.mp4.
When I send such URL to someone, I would like that browser start download, not play automatically.
Is it possible to compose URL in such manner to give browser instruction to start download instead of play it? With some parameter included maybe?
You can't do that by just sending the URL to someone.
What you can do is create a simple file which forces the user to download the file by setting the mime type of the response to octet/stream, which is the way of telling the browser the file can not embedded.
Below is an example in PHP taken from this website.
<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
I am implementing a file uplaod via jquery ajax but I fail to do something with the data on the controller side. I get the params but not the File which is in this example a jpeg. The data is in the Request Payload.
My problem is on server side. How do I get a File on server side?
#Secured(['ROLE_USER', 'ROLE_ADMIN', 'ROLE_PARTNER', 'ROLE_READ_ONLY', 'ROLE_USER_PAYING'])
def fileupload () {
println "---------------------------------------------------"
params.each{
println it.key +"="+ it.value
}
request.getHeaderNames().each{
println (it)
}
render("OK")
}
This is the output i receive:
---------------------------------------------------
filename=1506368_10152113826431683_327028558_o.jpg
apiKey=c7937acaf6d5411d8920d194dc48c041
action=fileupload
controller=post
host
connection
content-length
...
How do I get the file in my controller?
I'm doing the same, and using request.getInputStream() in the controller for getting the binary data. This actually comes from the ServletRequest interface - see http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getInputStream--
I need to set the save location in run time in mvc application . In windows appication we use the
System.Windows.Forms.SaveFileDialog();
But, what we use the web Application?
It is not clear what you want to save. In a web application you could use the file input to upload files to the server:
<input type="file" name="file" />
For more information about uploading files in an ASP.NET MVC application you may take a look at the following post.
If on the other hand you want the user to be able to download some file from the server and prompted for the location where he wants to save this file you could return a File result from a controller action and specify the MIME type and filename:
public ActionResult Download()
{
var file = Server.MapPath("~/App_Data/foo.txt");\
return File(file, "text/plain", "foo.txt");
}
There are also other overloads of the File method that allow you to dynamically generate a file and pass it as a stream to the client. But the important part to understand in a web application when downloading a file from a server is the Content-Disposition header. It has 2 possible values: inline and attachment. For example with the above code the following header will be added to the response:
Content-Type: text/plain
Content-Disposition: attachment; filename=foo.txt
... contents of the file ...
When the browser receives this response from the server it will prompt the user with a Save As dialog allowing him to choose the location on his computer to store the downloaded file.
UPDATE:
Here's how you could achieve similar in a web application:
public ActionResult Download()
{
var file1 = File.ReadAllLines(Firstfilpath);
var file2 = File.ReadAllLines(2ndfilpath);
var mergedFile = string.Concat(file1, file2);
return File(mergedFile, "text/plain", "result.txt");
}
I want to create a proxy controller in grails, something that just takes whatever is passed in based on a url mapping, records what was asked for, sends the request to another server, records the response, and send the response back to the browser.
I'm having trouble with when the request has an odd file extension (.gif) or no file extension (/xxx?sdcscd)
My url mapping is:
"/proxy/$target**"
and I've attempted (per an answer to another question):
def targetURL = params.target
if (!FilenameUtils.getExtension(targetURL) && request.format) {
targetURL += ".${response.format}"
}
but this usually appends .html and never the .gif or ?csdcsd
Not sure what to do as I might just write the thing in straight Java
Actually, the real answer was sitting in the post you linked to previously all along, by Peter Ledbrook:
Disable file extension truncation by adding this line to grails-app/conf/Config.groovy:
grails.mime.file.extensions = false
This will disable the usage of file extensions for format, but will leave the file extension on params.target. You can completely ignore response.format!