I have this following code for downloading files :-
#Controller
public class FileController {
#RequestMapping(value = "/files/{file_name:.+}", method = RequestMethod.GET)
#ResponseBody
public FileSystemResource getFile(#PathVariable("file_name") String fileName) {
return new FileSystemResource("C:/Users/sourav/fileServer/"+fileName);
}
}
When I go to the link for the first time nothing is displayed .When I reload only a text file with name f.txt is downloaded instead of the pdf file. I want the pdf file to be displayed in the browser. How to solve this problem ?
I think you need to set the response headers. Otherwise there is no way for the browser to intuit the file format. Something like response.setContentType("application/pdf");.
your code is ok. I think if you try with pdf file it will work as you expected, it will be displayed in browser. I tested it and worked fine in Chrome and Firefox. May be your testing file is corrupted.
If you are using Spring Boot, you can add the MIME types of the files you want to download into spring.mvc.mediaTypes properties in the configuration file. For example:
spring.mvc.mediaTypes.yml=text/yaml
Source: https://github.com/spring-projects/spring-boot/issues/4220
Related
When downloading a .xlsx file on a server, it automatically converts to .txt when asked for a save path.
You are currently working with SpringBoot, and if you click the download link through the tag in the view, click the .xlsx file.
The method of loading from the specified path.
The problem is that if you specify the href path for tag a as resource/static within the project, the .xlsx file will be downloaded without any problems.
However, if you route to a folder outside the project, the .xls file is downloaded normally, but. The xlsx file is automatically converted to .txt.
The path imported from the external folder is in the form of a request to the controller to return the actual file.
Below is the code that returns the file from the controller.
I'd like to ask those who know about this problem.
#GetMapping
public byte[] file(HttpServletRequest request) throws Exception {
FileInputStream in = new FileInputStream(new File().getPath() + "/filemanager/"
+ request.getRequestURI().split(request.getContextPath() + "/filemanager/")[1]);
byte[] image = IOUtils.toByteArray(in);
in close();
return image;
}
I am developing a Laravel application. I have developed a file download feature in my Controller like this.
public function downloadUsagePdf()
{
return Storage::disk('default')->response('path/to/usage.pdf', 'new-file-name.pdf');
}
As you can see above, I am renaming the file. I like to unit test that the file is rendered with the new name. How can I unit test that? I am not downloading. I am just rendering on the browser first.
The way I made it work was by doing this in the ControllerTest file:
$header = $response->headers->get('content-disposition');
$this->assertEquals($header, "attachment; filename=new-file-name.pdf");
Hopefully someone knows an easier way to do this
i have a string controller to download a xml file but the download window is open everytime..
this is the code of my controller :
#RequestMapping(value = "/export", method = RequestMethod.POST)
#ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException,
JsonMappingException, IOException, DatatypeConfigurationException {
byte[] bytes = service.exportXML(getUsername());
String xmlFileName = "filename.xml";
response.setContentType("application/force-download");
response.setHeader("Content-Length", String.valueOf(bytes.length));
response.setHeader("Content-Disposition", "attachment; filename="+xmlFileName);
response.getOutputStream().write(bytes);
}
What i can do to browser never open download window and save the file immediately?
Its not possible from server side. Due to securtity reasons browser will not allow to auto download the files if specified in the browser itself.
For example, if you want to auto download and open a docx file type in your browser. Then check this link How can I open these .docx files from Chrome more quickly
Hope you will understand the limitation here.
Try adding this:
response.setHeader("Content-Transfer-Encoding", "binary");
Well i have earlier tried for txt files which opened directly. Nw they get saved instead of opening.
I wrote a small app that creates downloadable pdf files with play 2.0
I want to serve them to the public. On my development environment I created a folder in the
/assets/ folder and everything worked nice.
Now, when switching to production, I figured out that play always deployed those files behind my back.
Do I really have to write a own controller to serve those files or what is the way here ?
I've also had problems trying to serve files created dynamically with the assets controller. I don't know if it's for some kind of caching but I ended up writing my own controller, and now it woks perfectly.
I mean, I use the Assets controller for regular public files and for my dynamic generated files I use this one:
public class FileService extends Controller {
static String path = "/public/dynamicfiles/";
public static Result getFile(String file){
File myfile = new File (System.getenv("PWD")+path+file);
return ok(myfile);
}
}
And the routes would be like this:
GET /files/:file controllers.FileService.getFile(file: String)
GET /assets/*file controllers.Assets.at(path="/public", file)
It works great for me
I'm trying to download the XLS file from this page: http://www.nordpoolspot.com/Market-data1/Elspot/Area-Prices/ALL1/Hourly/ (click on "Export to XLS" link).
However doing:
page.getAnchorByText("Export to XLS").click().getWebResponse().getContentAsStream();
returns the html of the web page, instead of the expected file.
Do you have any suggestion?
I already tried the 3 points here http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork without success.
The following:
webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
fixed my issue.