GuillaumeA
GuillaumeA

Reputation: 3545

Get filename as a request parameter

I try to load a file with node.js.

In my view, I've got a button :

doctype 5
html(ng-app="lineApp")
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    p filename: #{filename}
    button(onclick="location.href='/app/#{filename}'") click me

The page display a paragraph with filename: C:\users\username\my filename.txt. When I click on the button, the URL is something like http://localhost:8080/app/C:usersusernamemy%20filename.txt

So when I try to retrieve the parameter

exports.appli = function (req, res) {
    var filename = req.params.filename;
    //....
    });
};

with the server side call :

app.get('/app/:filename?', routes.appli);

I got an invalid filename. My question is then, how to pass a file path as a parameter in URL ?

Upvotes: 1

Views: 2345

Answers (1)

Matthew Bakaitis
Matthew Bakaitis

Reputation: 11990

This is a problem with the slashes acting as escape characters.

The first time you pass the string to the client, any escaped slashes (ex: c:\\users\\username\\my file.txt) are converted to single slashes.

When you use href.location, the slashes act as escape characters a second time...which is why they drop out when you try to call the server using it.

You could:

  1. Create two variables to pass to the jade template, one the filename as-is and the other an HTML encoded string
  2. pass the variables to the jade template:

For example, based upon your original jade:

body
    p filename: #{filename}
    button(onclick="location.href='/app/#{encodedFilename}'") click me

Upvotes: 1

Related Questions