Problems automatically converting to .txt when downloading xlsx files (excel files) within a Java (SpringBoot) server - spring-boot

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;
}

Related

Add a text file as a resource in Visual Studio 2022

I am creating a WebView2 app (WPF using C#) and I want to have a index.html file as a resource.
The idea is that when I create a project, a new folder is created.
This folder will contain an index.html file for the WebView control to load.
The index.html file then points to project specific data as relative paths.
I would like to have a file embedded in my resources that I can use at runtime like this
`void newProject()
{
string strHTML = /** code to read from resource file **/
createFile(projDir + "\\index.html") /** code to create a new file **/
/** code to write strHTML string to new index.html file **/
}
`
I know I can create a static string to do this, but editing is easier if I have the resource as a file.
Thanks in advance.
D
Update:
I put the file into the resources. Then, I added this code
var assem = Assembly.GetExecutingAssembly();
String[] resources = assem.GetManifestResourceNames();
html = assem.GetManifestResourceStream("index.html")
The resources string array always has two elements regardless of what I have in my resources file.
"WebView2Testing.g.resources"
"WebView2Testing.Properties.Resources.resources"
which has nothing to do with my file.
GetManifestResourceStream("index.html") returns null.
FYI I am using .net6 and WPF Windows app.
Any more ideas?

How to save a file to downloads using WinUI 3?

How do I save a file generated by my application to the downloads folder when using the WinUI 3 SDK? I know I can use FilePicker to select a file to use in my application but I would like to essentially do the opposite and save a file from my application somewhere or just simply put it into downloads.
This is an example saving the StoreLogo.png file, which is in the Assets folder by default, to the Downloads folder.
using Windows.Storage;
...
// Get the StoreLogo.png file as a StorageFile.
Uri storeLogoUri = new($"ms-appx:///Assets/StoreLogo.png");
StorageFile storeLogoFile = await StorageFile.GetFileFromApplicationUriAsync(storeLogoUri);
// Get the Downloads folder as a StorageFolder.
string downloadsFolderPath = UserDataPaths.GetDefault().Downloads;
StorageFolder downloadsFolder = await StorageFolder.GetFolderFromPathAsync(downloadsFolderPath);
// Save (copy) the StoreLogo.png file.
await storeLogoFile.CopyAsync(downloadsFolder);

Download a csv file to a shared location, instead of locally, using Spring Boot API

I'm using Spring Boot to download a CSV file (using OpenCV) with a GetMapping request. There is a forced timeout setup on the application server and my request on large files will timeout; 504.
I want to try downloading the file to a shared location and see if that helps (if you have any other suggestions I'm open to new opionions).
my question is, how to hit the API and download the file to a shared location rather than downloading it locally.
here is the controller.
#GetMapping("/download")
public void download(HttpServletResponse r){
string fileName = "xyz.csv";
r.setContentType("text/csv");
r.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
CSVWriter csvWriter = new CSVWriter(r.getWriter());
downloadService.build(csvWriter); //returns csvWriter
}

How to download files properly using FileSystemResource in Spring?

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

MVC big file download

I am using ASP.net MVC 4.0. I want to download a file after clicking a download button/a link. The problem is that the file is big and I want to show a 'wait image' while downloading. How do i do it? I am getting the file as a stream? Should I use HTPResposneMessage with web-api or FileStreamResult with MVC? The issue is I want to be notified when the download finishes.
My file is being downloaded with a code as below:
public FileStreamResult Download(Guid id)
{
.......
return this.File(cab, "application/octet-stream", id + ".cab");
}
I want to show a spinner in javascript before beginning the download. My code is as follows:
self.IsDownloading(true);
var url = '/Download/' + id;
window.location = url;
self.IsDownloading(false);
But IsDownloading(false) is being executed before downloading the full file. How to make sure that it is done after the full file is downloaded?

Resources