Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write PDF files from Web-App to USB-Stick

I am concerned with the feasibility of this: On a pre-configured machine I will have a Web-Application pre-installed, next to an Apache-Suite. So client and server are the same!

In this Web-Application Users can drag and drop PDF-Files to an USB-Icon.
Then the Web-App should write the dropped PDF to an attached USB-Stick.

I have never done something like this (writing to USB), so I am fairly insecure. And I am well aware of the browser-restrictions concerning JavaScript and Filesystem-Access, but...

after researching a bit I found out, that there might be some possible and
relevant (I'm a Web-Platform-Guy) solutions to this:

  • Make a "Chrome App" with USB-Permission (does this really work?)
  • Use PHP to find the USB and then write to it (how would that work under Windows?)
  • Use some Flash as middle man (not preferred)

Now I'd like to know:

  1. Has anyone some good experience with before mentioned possibilities?
  2. Has anybody ever done something similar? Did it work? Which path did you choose?
  3. How would I know which drive the USB is mounted, and how would I get sure?
  4. What other possible solutions to this problem are there?
like image 259
Sebastian G. Marinescu Avatar asked Jul 18 '16 16:07

Sebastian G. Marinescu


2 Answers

You have a website ('client-side' user interface) and a back-end server ('server-side') running on the same machine. This gives you 2 options:

  1. Client-side: Download a file through the browser via HTTP GET and let the user choose where they save it.
  2. Server-side: Build your USB interactions into the back-end (Node.js) code, as @mcgraphix suggests.

Interacting with the USB on the server-side provides the most flexibility. Furthermore, there are a number of libraries that you can leverage. Head to npmjs.org and consider, among others, the following Node.js server-side packages:

  • usb-detection
  • usb

With the server-side approach, initiate a Webservice request when the user completes the drag & drop action on the client, and implement the USB interaction within the server (Express.js or similar) method which services the request.

like image 57
Jared Dykstra Avatar answered Oct 13 '22 00:10

Jared Dykstra


If the letter of the stick is known then writing a file from PHP will be simple

file_put_contents( 'E:\\folder\\file.pdf', $data );

Update

You can read a list of drives into a dropdown and allow a user to select a default drive to write to

https://stackoverflow.com/a/8210132/696535

like image 39
Pawel Avatar answered Oct 12 '22 22:10

Pawel