My requirement is I should be able to download an excel template on a click event, I thought I will put that excel template in some folder(docs) in my backend code(instead of generating it dynamically) and download that. Is that possible, if yes how? I am using express and node.
It is possible.
Probably you use Express framework. Express can serve static files under '/public' folder.
When user connect to this file, her/his browser will download file. But some browser can view files online. For example, Chrome can open PDF files.
If you want to force to download file you can use this simple code;
app.get('/download', function(req, res){
var file = __dirname + '/upload-folder/dramaticpenguin.MOV';
res.download(file); // Set disposition and send it.
});
Related
Let's say for example I have a localhost server running on port 3000 and the url looks like: localhost:3000/index.html, how can I remove the .html extension without using .htaccess since it's on a localhost server. If there is no way without localhost then could you give me some way I could implement .htaccess in localhost? Thank you in advance
Edit: It is a site made in HTML, CSS and JS. Using a server.js file to do the backend stuff.
Most static file servers will by default implicitly serve index.html without needing to specify it in the path.
So the URL / will serve /index.html, and /somewhere will serve /somewhere/index.html.
Of course this depends on the software configuration.
I want to know how to download the files to aws-s3 through heroku, and dosen't use the web page.
I'm using wget in Nodejs to download audio files, and wanting to re-encoding them by ffmpeg. Though I have to store the file first, but heroku doesn't provide the space to store.
Then I find the way that heroku can connect with aws-s3, however the sample code is the page for user to upload, and I'm just want to using codes.
This is the code that haven't connected with s3 yet.
const wget = spawn('wget',['-O', 'upload_audio/'+ sender_psid +'.aac', audioUrl]);
//spit stdout to screen
wget.stdout.on('data', function (data) { process.stdout.write(data.toString()); });
// //spit stderr to screen
wget.stderr.on('data', function (data) { process.stdout.write(data.toString()); });
wget.on('close', function (code) {
console.log(process.env.PATH);
const ffmpeg = spawn('ffmpeg',['-i', 'upload_audio/'+ sender_psid +'.aac', '-ar', '16000', 'upload_audio/'+ sender_psid +'.wav', '-y']);
...
});
And it comes the error:
upload_audio/1847432728691358.aac: No such file or directory
...
Error: ENOENT: no such file or directory, open 'upload_audio/1847432728691358.wav'
How can I solve the problem?
Could anyone give some suggest plz?
Maybe won't use s3 can get it?
I don't recommend using S3 until you've tried the NodeJS Static Files API. On the server, that looks like this:
app.use(express.static(path.join(__dirname, 'my_audio_files_folder')));
More on NodeJS's Static Files API available here.
AWS S3 seems to be adding unneeded complexity, but if you have your heart set on it, have a look at this thread.
Im fairly new to node.js and express. My question is how to correctly retrieve json data in a client side javascript file since im getting a 404 code about the .json file not being found.
my file structure is a generic express structure and the .json file is in the nodeapp folder right below the package.json file and app.js. Im trying to access this file from a javascript file that is saved in public/javascripts folder but can seem to get around it. here is the function im trying to implement in the .js file:
function getJSONData(){
var json;
$.getJSON('/public/web_text.json', function(data){
json = data
});
}
Option 1
You need to setup static files in express, and you do it like this:
app.use(express.static(__dirname + '/public'))
You then can call it from your client (without the public prefix):
$.getJSON('/javascript/web_text.json', function(data){});
https://expressjs.com/en/starter/static-files.html
Option 2
Your other option is to send it via sendFile http://expressjs.com/en/api.html#res.sendFile:
app.get('/public/web_text.json', (req, res) => {
res.sendFile(__dirname + '/my_json_file.json');
});
Then in your client you can call it like this:
$.getJSON('/public/web_text.json', function(data){})
I need to perform those operation on a MVC application:
The user select a file from a grid
The file need to be saved on a shared folder
The user modifies the file with Excel
The file is saved in the same shared folder
A server deamon will process the file
My main problem (and I don't know if I can use handle it) is to prompt the user proposing to save the file on a shared folder...is this possible? or in alternative can I do via javascript to open the file knowing the shared folder and file name?
If you have hosted your application on client's IIS and from your localhost you want to save file to client end.
look at these code, maybe it will help you.
if (!string.IsNullOrEmpty(Path))
{
FileUpload file = new FileUpload();
string Folder = HttpContext.Current.Server.MapPath(Path);
string Path = Path.Combine(Folder, File);
file.SaveAs(Path);
}
but the path of folder must be shared..
Via a web app it is not possible to save a file locally on a specific location on a client's computer due to security restrictions, as far as I know this is true for on all known browsers. With a shared folder or a folder on another machine with a valid path that is accessible from the IIS server you can.
Try this in the controller to get the path of default Download folder on a client end:
string filename = "YourFile.xls";
string path = GetDownloadPath();
private static string GetDownloadPath()
{
String path = String.Empty;
RegistryKey rKey = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Internet Explorer\Main");
if (rKey != null)
path = (String)rKey.GetValue("Default Download Directory");
if (String.IsNullOrEmpty(path))
path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\downloads";
return path;
}
I am new to nodejs and expressjs
I am trying to develop a simple login page which has a username field and a password inside a form. on submit of the form i will redirect the user to a home page where i will display the message welcome user- userName. I want both the login and home pages to be simple html files. The form in login page has action set to home.html so when the user submits the form i can get the user name and password using res.body.userName and res.body.password. My question is how to use express to render html file. I do know that i can use res.sendfile function to send a html file but sendfile function does not support sharing variables to the html file.
I have posted below my code in app.js
var http = require("http");
var express = require("express");
var app = express();
app.use(express.bodyParser());
app.all("/",function(req,res,next){
res.render("login.html");
});
app.all("/home.html",function(req,res,next){
res.render("home.html",{"userName":req.body.userName});
})
http.createServer(app).listen(3000,function(){
console.log("Listening at port 3000");
})
You don't need a rendering engine to serve up static assets like html files. You should rethink what is being served up on different request but for what you want you have to place your static assets in a directory and tell express where they are like so
app.use(express.static('public'));
Assuming your server file is on the root, and you have an public folder on the root as well, put your html files in the folder. By default, express will look for an index.html file in the foot of that public folder and serve it on a GET request to /. You are overriding that by serving your login page. That's fine. You have to use res.sendfile('/path/to/file') instead of res.render()