1

The following works fine if I need to serve the files which are of text types like log files, text files, etc...

But I'm unable to serve word specific files or any pdf/image files.

 function ShowFile(strPath){
    if (objHttpFileDataRequest){
        objHttpFileDataRequest.abort();
    }
    objHttpFileDataRequest = $.get(
        "includes/main.cfm",
        {
            path: encodeURI(strPath)
        },
        function(strFileData){
            $("#readerArea pre#filecontent").text(strFileData);
        }
    );
}
var objHttpFileDataRequest = null;

I do have relevant code for handling pdf, images, and doctype files on my server, I only know that this jQuery should do something, I think that there is something missing.

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
John
  • 15
  • 4
  • 2
    The `$("#readerArea pre#filecontent").text(strFileData);` works for plain text files, not binary files like pdf/images. – Asons Oct 01 '18 at 18:21
  • right, so what should i do for other types of files? – John Oct 01 '18 at 18:22
  • Encode it as base64 before downloading, then use one of these solutions: https://stackoverflow.com/q/3665115/519413 – Rory McCrossan Oct 01 '18 at 18:24
  • You might find these two links useful, 1: https://stackoverflow.com/questions/33902299/using-jquery-ajax-to-download-a-binary-file ... 2: https://www.henryalgus.com/reading-binary-files-using-jquery-ajax/ – Asons Oct 01 '18 at 18:24
  • And do note, for a user (client browser) to be able to read/show binary files like a pdf (can be viewed with a javascript library though), it needs to have the proper plug-in installed, which is out of your control. – Asons Oct 01 '18 at 18:27
  • i have server code for viewing, i just need it to go through the url once i get the file, i can server it as its type – John Oct 01 '18 at 18:29
  • "Server code for viewing" won't work client side. – Asons Oct 01 '18 at 19:00

1 Answers1

0
  1. Get the extension (From this answer: https://stackoverflow.com/a/3042353/1410567)
  2. If it's txt or log files (or, add other files), show the plain text output

else

  1. If it's not plain text as delineated above, redirect the browser (or open new window) to the file:

 function ShowFile(strPath){
    if (objHttpFileDataRequest){
        objHttpFileDataRequest.abort();
    }
    objHttpFileDataRequest = $.get("includes/main.cfm",
    {
        path: encodeURI(strPath)
    },
    function(strFileData){
        openFile(strFileData); //assuming strFileData is a string with a URI path
    });
}

var objHttpFileDataRequest = null;

function openFile(file) {
    var extension = file.substr( (file.lastIndexOf('.') +1) );
    switch(extension) {
        case 'txt':
        case 'log':
            $("#readerArea pre#filecontent").text(strFileData);
        break;     

        default:
            window.open(strFileData, "_blank");  //is not plain text, open in new window - this may be blocked by pop-up blocker protection in some browsers
    }
};
Sean Kendle
  • 3,538
  • 1
  • 27
  • 34