Javascript: Save uploaded file - javascript

I would like to save uploaded file using javascript, in my linux server. The code I wrote is:
if (uploadInput.files.length == 0) {
console.log("No file is uploaded. ");
} else {
console.log("File uploaded.");
var file = uploadInput.files[0];
}
Now I would like to save that file as "files/upload.csv". Can anyone please advise, how can I do it?
Thanks in advance

What I'm going to do is walk you through the logic, instead of providing code. There is just not enough information here on what you want to do to provide actual code and the sample you provided is a very small part of what the actual solution would need to include.
I'm assuming the code you wrote above is meant to run on a website visitor's browser (client-side). Client-side code can't save to a server. What it can do, is send the file contents to the server. But then you'd need something on the server side to process that file contents and actually save it to the server-side files directory.
One method to send the file contents from the client to the server is to use AJAX - you can do this with native javascript, but I would recommend looking into a library such as Jquery, which makes it a lot easier. See http://api.jquery.com/jquery.ajax/ This AJAX code will need a communication point on the server to send the file contents to. From your profile it seems you're familiar with PHP. You could make a php file on the server (say receivefilecontents.php) that takes in input from that client-side AJAX call, and then saves it to a server directory - you could also do this in Python, Java or a number of other languages.

Related

How do you run a server-side script without it being sent to the browser at all?

I understand that the title is a little confusing, but essentially I want to make my website run a php file on the server side that uses arguments from the html e.g
document.getElementById("example").value;
So I'd like to run it on the server but not have it linked to the html file. Is this possible?
I suggest you create PHP scripts on server side and reach them with XMLHttpRequest (tutorial here) or AJAX with Jquery for example.
PHP scripts you need, don't have to generate any HTML code : they just would process data.
In your JS, you get your values from the page, then you send your data to the server.

Check uploading file is password protected or not in Javascript or jQuery

Please help me on this.
I have some file upload functionality in my application which is implemented using Javascript/jQuery on the front-end and an AJAX call to send the file object to the back-end written in Java.
I need to detect if the file uploaded is password protected. If it is then I want to throw an error to the user.
Is there an option to check this on the front-end using Javascript/jQuery or should I do it in the back end?
In my Java middle-ware I am using the SmartUpload class to fetch the file object and insert it into a DB (blob format).
Please suggest which is better and why.
Thanks in advance.
Here is how you can check if a pdf file is encrypted or not in java.
See the sample below
try
{
document = PDDocument.load( "C:\\abc.pdf");
if(document.isEncrypted())
{
//Then the pdf file is encrypeted.
}
}

How to send a javascript file (with several functions in it) from the server to the client side?

I need to have the functionality in the server side in order to hide the implementetion to the final user.
I didn't find a topic with this kind of solution.
I have a .js file with functions I use within the html5 file.
The js files are "called" in the html by using the script tag, but through the url the user can track them and see the .js file content. I don't want this to happen.
$getScript() does the job, but again the url can be cathched, thus the file content too. Much the same with $ajax function.
Everything work ok, but I want to hide the js content.
The .js file is something like this:
var x, x,....
function A(){...}
function B(){...}
and so on, I use A(), B() functions in the html.
Which is the best approach to get the content file from the server without doing the url visible?
Server: nodejs. (I send some json files through socket.io correctly, but I don't know how to achieve this other issue.
Thanks in advance, best!
If you are sending sensitive information to the client then you are doing it wrong. No matter if the client has the URL to the script, they will still be able to find it if they are determined as long as it is sent to their computer.
Find a different way to accomplish what you are trying to do without sending sensitive information to the client. It is not safe.

Save javascript output to a file?

I have php made website. It runs on windows machine. I have a javascript that i have tested that gets me the adobe version used by clients. The problem is that by running the code I'm able to retrieve the Adobe Reader version but cannot saved the output to a file on my server end.
Here is the Javascript code. I took this code from sister stackexchange website.
http://jsfiddle.net/EGbY5/3/
What is the best way to save this information from js? Should i use js or any other scripting? I read this from google that you cannot use js to save files on server? If this is true is there any other way.
I would suggest using AJAX (or a form, if you want the user to explicitly know this is happening) to send a request to your server side code detailing the information you have collected with the script.
The advantage of AJAX is that you can do all of this without the user being explicitly aware of it. The disadvantage is that you rely on their browser supporting Javascript, but since you are already collecting information using Javascript this problem is mute.
Ajax'll do it.
JavaScript source:
var adobeVersion = CheckAdobeVersion();
$.post("script.php", {version: adobeVersion}, function(){});
PHP source:
<?php
$version = $_POST['version'];
write all text to a file("my file.txt") // Pseudo code...
?>

Count number of files in a folder through javascript

I have folder in which there are no. of html files. Can i count the no. of files using javascript? Please help
Thanks
No, this is not possible using JavaScript only.
You can make an Ajax call to a page and from the server side code you can find the number of files in a folder and then return the result to the callback function.
I assume the files are in not in the client machine.
Javascript is a client side language. This means that it has no interaction at all with the server. It get sends unprocessed by the server to the client and the client executes it.
However, what you could do is learn AJAX - if you don't know it yet - and create a script (PHP, ASP, Perl, SSI, etc.) that counts the files in the directory and prints the number - I recommend Perl - .
Each minute, for example, the AJAX page would fetch the response of the script and display it.

Categories

Resources