Download files from folder with webmatrix - razor

I have a folder created to get the files that users upload. However when i try to download the files, some extensions don't download.
I have this code to list all files that are in the folder, i can see all the files with no problem.
#foreach (string fullFilePath in Directory.GetFiles(Path.Combine(Server.MapPath("~/uploadedFiles"),"Ticket Id - "+#id)))
{
<div class="linkFicheiros">#Path.GetFileName(fullFilePath)</div>
}
With this line
#Path.GetFileName(fullFilePath)
i can download files with extension: ".zip" , ".xls" but extensions like ".msg"(sometimes users need to upload this extension) i got an error "The page cannot be found". Even ".jpg" instead of downloading the file it opens the image on the browser.
I think that is something how i'm trying to reach the file, but i can't get to a solution.
Any thoughts ?

The browser would try to always view the content of the file. If its an Image file like .jng etc. But if there is a .zip file, it would let the user download and open it. Because browser can't open it.
You need to push the file to the user. For that you can try the following code:
var file = Server.MapPath("~/images/" + Request["img"]);
Response.AppendHeader(
"content-disposition", "attachment; filename=" +
Request["img"]);
Response.ContentType = "application/octet-stream";
Response.TransmitFile(file);
Now, you can see that in the code I am sharing, the file is a variable, which is being pointed to the file in the File System. Please note that there is a Query String parameter, which would be sent alongwith the URL, for example:
Download Image
Now the header would be appened, and the
Request.ContentType = "application/octet-stream"
is used to force the browser to show the Dialoug of Open/Save.
Then the transmit file to download it.
To only execute the code when needed
To only execute that download code block, you need to set the value in a block for condition to be true. For example, you can use a parameter to check whether to download the file or not. Try something simple like,
<a href="~/download_file/image_link.png?download=true>Download</a>
Then on the code behind use this:
var download = Request.QueryString["download"];
if(download == "true") {
/* place the code here */
}
Now it would only execute if the condition is true, otherwise it would skip that part.

Related

How to download a file generated "on the fly" with Puppeteer?

I want to download and rename a PDF file that is generated "on the fly" by an Angular app.
With this code, the PDF file is downloaded but I want to rename it before writing to disk.
//Download PDF
await secondTab.waitForSelector('img[title="Format PDF"]', { visible: true, timeout : 10000 })
await secondTab.waitForTimeout(10000);
var current_element = await secondTab.$('img[title="Format PDF"]');
var parent_node = await current_element.getProperty('parentNode');
await parent_node.click();
console.log("Downloading PDF");
I wasn't able to find a solution. All solutions talk about files downloaded from URL.
I tried to trace events on the page (cf. PageEventObjet), but no one catch this "on the fly" generated PDF file.
My question : is there a way to catch that stream, name it and save it to disk ?
Thank you very much.
Note : I dont want to play with the File system (i.e. find my downloaded PDF, rename it, move it, etc.)

Puppeteer: how to rename a file before downloading?

I want to crawl some file using puppeteer, and I find the downloading practice like below:
puppeteer - how to set download location
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: dirname,
});
but I want to change the filename now, some examples use "fs" module to rename it when download finished, but usually, there are many files and different names, it couldn't check it in PC file system manually, we want to click the download button and rename it at right, how can I reach it?

Create download link for static asset file

I would like to put a download link on my website so the users can download the file template.csv. The file is static and is located at the root of my /grails-app/assets folder.
Inside my page.gsp, I have tried 2 methods do so, to no avail :
Download the template
this results in a template.csv file being downloaded, but the content is the html code of page.gsp rather than the original content of the file I uploaded in my assets.
Download the template
the generated html file has a link to localhost:8080/mysite/assets/template.csv but clicking it prompts an error message : Failed - no file.
What is the correct way to do what I want to achieve ? Is there an issue with extra permissions I would need to add to allow the download of my file ?
Our webapp relies on a rather old technological stack :
Grails 2.3.4
Plugin asset-pipeline-2.2.5
I have had some troubles with downloading files straight with a -tag.
To overcome this, I use Controller method to return the file and place the downloadable files in their own folder under web-app/:
class MyController {
#Autowired()
AssetResourceLocator assetResourceLocator
def downloadExcelTemplate () {
String fileName = "MyExcelFile.xlsx"
/* Note: these files are found in /web-app/downloadable directory */
Resource resource = assetResourceLocator.findResourceForURI("/downloadable/$fileName")
response.setHeader "Content-disposition", "attachment; filename=${resource.filename}"
response.contentType = 'application/vnd.ms-excel'
response.outputStream << resource.inputStream.bytes
}
}
And just use regular a-tag to to link to this controller method.
This way you also gain more control over file downloads.

how to get browsed file location by user when he wants to download a file

this is my html code to make user to download a file and it is hitting controller
window.location.href="#routes.ListManagementController.downloadList("+listName+")?listname="+listName;
this is my controller code:
String listName = Form.form().bindFromRequest().get("listname");
response().setContentType("application/x-download");
response().setHeader("Content-disposition", "attachment;filename="+listName+"_data_export.csv");
The above two respose() statements make a pop up to download a file I want the browsed file location
File file = new File("C:/csv/" + filename);
So, using servlet api we can write the content into browsed file location using respose.getOutputStream() method. In play there are is no support for servlet. I want browsed file location selected by user so that i can give that location to File and write the file over there.
You can't get the location of a directory on the client, and even if you could, your server side could wouldn't be able to write to it (since it would usually be on a different computer).

How to get a directory path from the client using html form

I am developing some web app to be used on my local pc only.
The question is that I want to be able to browse to some directory or file and get its path. So is this possible using some file/directory path picker (not the whole dir, just its path).
I don't want to manually type the path in the text input, rather to have some visualized way to navigate to the dir.
Thanks
Previous Removed
EDITED:
$("#someFileUpload").change(InputChanged);
function InputChanged(e){
var fileList = e.target.files;
//You now have a list of the files uploaded, from here you can work with the objects..
}