Send files without button event - javascript

Is it possible to send files without choosing them with button click and searching folder? I have app written in ReactJS that chooses files on button click, but for testing purposes I need to have 'hardcoded' files so that I don't need to make any actions in my application. The question is: can I do something like that within React app?

First of all, you can't read files from the client computer, even if you have the full path. That would be a major security flaw if javascript could access client files without the explicit consent of the user (provided by the browser).
You can, however, create a file within javascript with hardcode content and send it by http request. Here you have a solution on how to achieve this.

Related

How to retrieve file names from a specific directory using JavaScript or jQuery?

I want to make a code using only JavaScript or/and jQuery to access a static directory and retrieve the names of some icons i saved there (SVG icons) and display the names to the user. i couldn't do that with the file api and i have no idea where to start.
I'm going to assume from your mention of jQuery and the File API that you're trying to do this within a browser.
You can't. It just isn't allowed, there is no mechanism to provide it.
If you're in control of the machine where you want this information to be accessed, you can run a server process on it that can do that; code in the browser can then make a request to the server code to request the information. But there's no browser-only way to do it.

Extract file name with directory structure for internal web site

Environment:
html5
JavaScript
Angularjs
node.js
express.js
Couchbase
Question:
I understand the concerns and security measures implemented within the web environment to prevent the display of directory paths to the world. However, I have an issue that requires knowing the full directory path to a selected file.
I am building a web page for an internal website. The web page needs to allow the user to select a tab delimited file. This tab delimited file will exist on a network server, which is a policy instituted by the company and mandated by external auditors. This file may exist for various clients, with data specific to the client. With that said, the files will reside within different folder structures on the server(s). The user wants to pick the appropriate file and have the data uploaded to the database. Based on the size of the file (up to 10’s of millions of rows), the user does not want to wait for the web page to process immediately. Therefore, the solution is to create a task. The task will contain all the parameters necessary to manipulate the data prior to uploading the data to the database. I understand the simple solution is to upload the file to a common directory but that is not practical. As the user could set up several tasks that will upload the same tab delimited file to the database using different parameters.
I would like to have my task creation process contain the file name with the directory structure. When the background process executes the task, it can extract the data from the original location. Additionally, if I have multiple tasks extracting the same data, I not concerned I may have multiple copies of the data present.
I will appreciate any help with code snippets, website, etc. that may suggest methods to resolving this issue. Please not, that at the current moment, PHP is not an option. A management decision prevents the use of PHP.
TIA
Anthony
It is not exactly possible, so I see two solutions. One is to try and get the path to the temporary location of the file, rather than the actual permanent location.
Suppose your has an id of fileInput and you're using jQuery:
$('#fileInput').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
console.log("Temporary Path(Copy it and try pasting it in browser address bar):"
console.log(tmppath);
});
Otherwise I would just make a separate input for path, and show a brief instruction to users on how to copy-paste the URL from their Windows Explorer window.

Is it possible to generate a text or xml file from local storage data and save it in a folder?

I'm working on a website that is going to be offline. All the html files will be in a folder stored on the hard-disc. I've managed to do 90% of the work and the last part I have no idea of. Here is what it is:
I have stored a list of products in the localStorage as various strings under the keys - like buying objects and it goes to the cart, the cart objects are in localStorage. I created a page that showed the list of all the products in the localStorage. It can be cleared if the user clears them. Now I need to create a page where all the objects that was selected before, regardless of the localStorage being cleared, show as list in this page. You can take it as the page that lists products that have been ordered in the past, i.e even after the cart is cleared the products will show in the past-orders page.
I do not know any server side codes, I did everything using JavaScript as it was supposed to be a simple project, but I'm stuck at this part. So I cannot use PHP or anything to generate files or use a database to store stuff.
Here's what I thought but I don't think it works but wanted to confirm if it does or not:
Generate an XML file or a .txt file and store it in the drive and then clear the localStorage. But I don't think it is possible. If its possible just using JavaScript please point me in the right direction and I'll research and come up with something.
P.S. the website will be entirely offline what I mean is the users will never connect to the internet for this to work. Also there won't be a server or localhost.
Thank you!
The site is completely offline, but functionality is similar to an eCommerce site. You click a button and some content from the website stores in the localStorage and I have to call it in multiple pages, when a user clicks another button, localStorage clears but whatever was selected before must be available without localStorage. Hmmmm.. Consider a quiz site where you answer everything and when you take a new quiz, old scores will be stored somewhere else and it won't change when you take a new test.
Is it possible to attain this functionality without a server side script? It seems the final-targeted-users won't know how to install any softwares that can provide a localhost or a server or something like that.
Client-side, browser's JavaScript runtimes don't have local file system access excepting a very limited one when uploading files to a remote server, and anyway, browers won't give you direct access to the file.
You'll need some kind of server-side logic behind the scenes if you want full I/O in the local file system.
Perhaps you can take a look at NodeJS, a server-side JavaScript runtime that can work as lighty Web server on either small and large projects, and it has built-in server-side JavaScript I/O functions.
Otherwise, you're absolutely stuck in the client-side Web browser's sandbox limitations.
U can refer documents of knockoutjs and NodeJS.. That would probablky help... A far as my knowledger is concerned NodeJS does contain a way to handle your problem.

Python - Transferring session between two browsers

The issue:
I have written a ton of code (to automate some pretty laborious tasks online), and have used the mechanize library for Python to handle network requests. It is working very well, except now I have encountered a page which I need javascript functionality... mechanize does not handle javascript.
Proposed Solution:
I am using PyQt to write the GUI for this app, and it comes packaged with QtWebKit, which DOES handle javascript. I want to use QtWebKit to evaluate the javascript on the page that I am stuck on, and the easiest way of doing this would be to transfer my web session from mechanize over to QtWebKit.
I DO NOT want to use PhantomJS, Selenium, or QtWebKit for the entirety of my web requests; I 100% want to keep mechanize for this purpose. I'm wondering how I might be able to transfer my logged in session from mechanize to QtWebKit.
Would this work?
Transfer all cookies from mechanize to QtWebView
Transfer the values of all state variables (like _VIEWSTATE, etc.) from mechanize to QWebView (the page is an ASP.net page...)
Change the User-Agent header of QWebView to be identical to mechanize...
I don't really see how I could make the two "browsers" appear more identical to the server... would this work? Thanks!
Since nobody answered, I will post my work-around.
Basically, wanted to "transfer" my session from Mechanize (the python module) to the QtWebKits QWebView (PyQt4 module) because the vast majority of my project was automated headless, but I had encountered a road block where I had no choice but to have the user manually enter data into a possible resulting page (as the form was different each time depending on circumstances).
Instead of transferring sessions, I met this requirement by utilizing QWebViews javascript functionality. My method went like this:
Load page in Mechanize, and save the downloaded HTML to a local temporary file.
Load this local file in QWebView.
The user can now enter required data into the local copy of this page.
Locate the form fields on this page, and pull the data the user entered using javascript. You can do this by getting the main frame object for the page (QWebView->Page()->MainFrame()), and then evaluating javascript code to accomplish the above task (use evaluateJavaScript()).
Take the data you have extracted from the form fields, and use it to submit the form with the connection you still have open with mechanize.
That's it! A bit of a work-around, but it works none-the-less :\

I need to generate a JSON file in browser using (currently) javascript

I have a website that allows users to select a date range from a data set. At least, that's the goal.
What I would like to have happen: the user selects a date range, presses the submit button, and a script generates a JSON file which MATLAB reads to generate the graphs.
Any thoughts on resources to help accomplish this?
You'll need the script that fires off to be server side. JavaScript is client side and can not, in any way, access, modify, or otherwise create files on the client. You'll have to use a language like PHP to create the file.
Example using PHP:
Once the file is created, force a request on the client side to fire asking for the file. Set the PHP header to Content-Disposition: attachment; filename="< Place file name here>".
This will prompt the browser to launch a download prompt allowing the user to download the file.
Hope this helps.
You can use Downloadify, a small Flash component with a Javascript interface that allows you to create files on the client that a user can download. That's what I used in a similar situation.
You could also try and use Data URI but they are a quite limited and browser specific so some issues may arise.
These may be alternative solutions to the previous answer that suggested using server side code to generate the file.

Categories