1

"+" sign in path gets converted to space

The code snippet below works just fine until my file path contains a "+" symbol, whereupon, when it reaches Download.php it has been converted into a space

    $('#ViewPdf').on('click', function() {
        var file = $('#PdfPath').val();
        if(file) {
            $(location).prop('href', 'Download.php?DeleteTarget&download_file='+file);
        }
        else { alert('No pdf file so cannot view'); }
    });

I am unsure whether the problem is a JavaScript one or php. Some sort of escaping or character conversion looks to be necessary but I am unsure what.

NormB
  • 41
  • 5

2 Answers2

3

Try using encodeURIComponent() :

var file = encodeURIComponent($('#PdfPath').val());
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
2

You can use encodeURIComponent at the initialisation of your variable file.

Like that you encode the + sign as %2B

var file = encodeURIComponent($('#PdfPath').val());
R3tep
  • 12,512
  • 10
  • 48
  • 75
  • 1
    Please don't copy other peoples answer, it's not nice. Your edits can be seen by clicking the 'edited ..... ago' link underneath your answer. Anyway, you got your points on the old answer, so why change it? – KIKO Software Apr 15 '19 at 19:42