Open PDF files using PDF.js without passing file as query string - javascript

I am using PDF.js to open a PDF file currently and its working fine.
Previously I was using like this:
http://myapp.mycompany.com/web/viewer.html?file=http://myapp.mycompany.com/my-PDF-file.pdf
My Problem:
For some reasons, I really do not want to reveal the file path of my PDF file and want to move my PDF files outside root directory. Is there any other way to provide the pdf file to the viewer.html? or is this the oly way?
NOTE: I have moved all the PDF files outside my root directory. So How can I access the PDF now?

Yes, there is. Call some function instead (and you don't even need PDF.js):
http://www.example.com/getPDF.php?fileName=pdfname.pdf
function fileName() {
$fileName = $_GET['fileName'];
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='{$fileName}'");
readfile(__DIR__."/../pdfs/private/{$fileName}");
}

Use rawurlencode to encode '/getP‌​DF.php?filename=pdfname.pdf' part:
"http://myapp.mycompany.com/web/viewer.html?file=".rawurlencode("/getP‌​DF.php?filename=".rawurlencode("pdfname.pdf"))

Related

replace complete HTML file content from javascript

I have a html file under my war folder in the project ,
I just want to access that file From the path "/myhtml.html"
and then replace whatever is in this file from a new text "Some new Text"
so that My html file now will only have "some new text" there .
I searched alot for this , but they provide example for changing a specific div etc in a file , but i just want to replace everything in a file which is present under a war folder.
I understand that you are trying to change the content of HTML files using Javascript that is running in the browser.
Front-end Javascript can't access local files that are stored on your machine. It would be a security disaster if it could. The only way Javascript can read with local files is using a file upload input, and it will only be able to read the files not update/overwrite them.
NodeJS however is capable of accessing the local file system. NodeJs is backend JavaScript so it will be running on a server.

JS/TS: Reading files from the directory I open a HTML file in browser

I have a project where I visualise data from a file (say, a CSV). My visualiser is a .html bundle created with Webpack. Up until now, I include a placeholder for the data as a string in the bundle. If I want to change the data, I replace the placeholder in the bundle .html file with the new data I want it to show (via an external script).
Now I'd really love to change this for convenience. For example, I'd like to get a list of all the supported files that are in the same directory as the .html file and load their data, either in the background or when I click or them or whatever. (In the easiest use-case, there would just be 1 data file in the directory and the project would just load this data automagically at startup.)
I had a look at Handlebars.js and I think it might help with the injection of the data. However, it seems that there is no possibility to read from the directory I start the .html file from. I already tried fs and path but those don't work in the browser when I just want do double-click the .html file.
Is there a possibility for the browser to read files from the directory I open a .html file from? And if so, how can this be done in Javascript/Typescript?

How can I create folder on localhost using jQuery and insert imageData as Image at that folder?

How can I create folder on localhost using jQuery and insert imageData as Image at created folder?
My localhost directory is
E:\xampp\htdocs\trinity_app\desktop\BAT\res\images\deliver\
I want to create a new folder in this directory. Folder name \new_folder.
you can not create folders by jQuery. jQuery is a JavaScript library that can not write to your filesystem. It would be a huuuge security risk to allow JavaScript to write to your system.
For such tasks, a server-side script language like PHP is just perfect. If this actually was your question...please modify it and have a look at the other answer posted here. On the client-side, there gladly is no way to do so by JavaScript
Using this you can make a directory,
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}

How to create a popup window to select a file in a local directory?

I want to create a popup window that shows me all the files in a certain directory like ex a /functions directory. I want to be able to click a file in that directory, click ok, and store it's info in variables (not upload it), How would I do this?
I've tried using a form as:
<form action="" method="post" enctype="multipart/form-data">
But with that I can't specify a directory to open specificlly, I have to manually navigate to that directory.
I've tried using window.open()
window.open("file:///" + "home/user/Desktop/demo/functions");
I've tried using an onclick link mechanism:
<a onclick="file:///+ "home/user/Desktop/demo/functions"">Open folder</a>
None of these seem to work, any ways I could approach this problem?
In JavaScript, file handling gets a bit messy. The only way to grab the contents of a folder from JavaScript would be to connect to a server and have a serverside code in a different language relay the folder information back to JavaScript.
The only way I can think that we could be able to fake this result is by placing an index.html file inside of the target directory. This index.html file would then have the names of all the files in the folder within it. However, they would have to be manually plugged into the HTML file. (if you know how to use PHP, it can scan a directory and push the contents to the HTML file)
When a web browser has to navigate to a folder, it asks the server for an index file (usually this will be an HTML or PHP file). This index would then have the contents of the folder inside of it.
If the folder is indeed on the local computer, however, there is one final way we can do this...
If the page navigates to a folder using a window.location of something akin to file:///C://Users/USERNAME/Desktop/My%20Folder/, chrome (or whatever browser you are using) will navigate to the directory and display the contents of the directory. However, since you can't put JavaScript into this browser-generated index page, you won't be able to manipulate it.
The <input type="file"> is probably your best bet, but you can't set a default directory with it (at least not without some JavaScript voodoo, and even then there are security issues between the web and the local user).
I don't know why you would want to do that, anyway, since directory structures are going to be different between different users, and the specification of paths is different between different OS's.
Instead of doing this I will suggest you copy that picture in project/image folder after clicking the upload button.

JavaScript API for Adobe Acrobat - how to create a relative link to a non-PDF document with JavaScript?

My company wants to set up some PDF documents to track projects. We want to create links inside the PDF that, when clicked, open a given document (.doc, .xls etc) using a relative path.
There is the app.openDoc JavaScript method, however that only works for PDF files.
There is the "Open a file" action, but that seems to work for only absolute paths.
I've looked into the API ( http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf ) to no avail ... does anyone know how to do this?
The doc object has no such method, but you can do the following:
Convert the .doc or .xsl file to .docx or .xslx
Attach the XML file to the PDF
Use the Doc.getDataObjectContents() to read the data
As an alternative, you can also use the util.readFileIntoStream method in a closed environment where you can install folder-level scripts on everyone's system.

Categories

Resources