Sorry If this is trivial,
Edit: in short I found out this really isn't intended to be done with client-side JavaScript.
I am wondering if i know a folder has pictures. /home/project1/pictures
and they follow some naming convention face102039.jpg face1030303.jpg ...
Is there a was to know every picture thats in there starting with face but not knowing what numbers are after it? Like is there a way i could possibly grab all the file names with a .jpg or .png extension and then i would know how to parse their strings. I am just not sure how to get all the files in a directory with javascript.
I saw this similar post, and I know how to do what I want in Java, but i couldnt find a way to do this in javascript. I was thinking about doing it server side with java or ruby but I am pretty sure i should be able to do this clientside via javascript. Maybe I am wrong though.
Given the constraints it is technically possible to do this, just not very practical. I would not recommend using this since you could lock up the user's browser pretty quickly.
var len = 2000000;//How long you want to wait.
var pics=[];
for(var i=0;i<len;i++){
a=new Image();
a.onload=function(){
pics.push(this);
}
a.src='/home/project1/pictures/face'+i+'.jpg';
}
The real answer is you need to implement a back end in PHP, RoR, etc. To send you a list of the files in the picture folder. This can be done with AJAX.
Javascript is running at the client side. For example in the browser of the visitor. It has no direct access to the server and image folders, as Java and php does.
What can be done is using ajax in the javascript, to fetch a java/php file which lists the directory content.
You can't access the file system with JavaScript. You could, however, use AJAX to query your server for that information and return it as a JSON string.
Hope this helps.
Related
I have a relatively simple task that I want to achieve, but I am totally stumped on how to achieve it. I want to create an HTML page that allows users to select a local JSON file. I want to pass the path to this local file to some Javascript code. Then I want to read that file using Javascript every time the file changes.
I've investigated a few different methods for achieving this. First, I tried just using Javascript. This worked great until I wanted to read the file multiple times as it was changing. Evidently there are security concerns that make this impossible.
Next, I investigated using Node.js to solve this problem. It has some file utilities that seem perfect, and it's server-side, which may help with the security issues. But using just Node, I couldn't figure out how to select a local file in HTML and pass the path to the Javascript code. From investigation online, it seems like Express.js would be useful.
So now I'm trying to use Node.js and Express.js. Now I can figure out the file name from an HTML form! But I can't figure out the path to the local file so that I can read it using the Node file utilities... From looking at similar questions online, it looks like everyone uploads files, then reads them. Is that what I have to do with Express? If so, is it possible for me to monitor changes to the original file, upload the file when those changes get made, then read the changed file? Is what I'm trying to do possible? If so, does anyone have any recommendations on libraries/modules to use? Thank you so much for any help - please let me know if I can be more clear about anything.
Do you know if there is a way to create a javascript or PHP file in Umbraco?
By this I mean that I would like to have the user populate fields in the same was as creating a page, but for it to output as .js or .php rather than the usual .aspx
Many thanks!
I don't see any reason why you couldn't; you could use a controller to output as pretty much anything by setting the mime-type.
I'd be wondering though what your use-case for generating PHP files would be though; the server would still need to process the PHP script somehow; which may not be a trivial task (unless you just want to generate the PHP file for download/display the code) as it isn't client-side script at all.
JavaScript on the other hand is entirely a different matter - as it's processed client-side you could just point the browser at a url that happens to deliver the generated JavaScript. You could even render the JavaScript out from a WebApi end-point and the browser client would quite happily consume it.
For CouchDB, I know that show function can generate HTMLs / Images / XML feed on the fly.
While in that case they have to be in the script itself and encoded (e.g. base 64 for image), as in here
What is the best way to load static resources which are attachments of design documents
e.g. As simple as JSON, or Images and process with server-side javascript?
The script file itself is an attachment in the design doc. The variable doc is not available.
Are there any way similar to node.js for it? or we use trick in context like _show or _list to show the document with id: _design/ddoc ?
doing REST request inside that environment I believe is also not possible as XMLHttpRequest is also not available. Establishing DB connection is also not possible?
This supposed to be a simple question, I wonder I am missing something in couchDB?
In order to serve a website directly, you need to use url rewrites. You'd rewrite / to got to one of your show functions. to bootstrap your site with basic HTML and JS (embedded probably).
A lot of this work has been done already by CouchApps (basic tutorial here). This is by far the easiest way to get started. This seems to be the way http://npmjs.org is served.
This isn't the place for a walkthrough, so hopefully this gives you enough information to get started.
If your site needs server-side logic (websockets for example), this solution won't work for you. All you get with a couch app is a database, HTML, CSS and Javascript.
This is a file stored locally, not on a server, so Server Side Includes do not work.
Problem:
I have an HTML file. There is lots of data in it, I want to split it into smaller parts, and then just include them all into my big html file, i.e. something like:
main.html
<include "partA.html">
<include "partB.html">
<include "partC.html">
And I want the result as if the contents of partA,B,C.html were read right into main.html
Now, this is not on a server -- it's stored locally, so I can't do SSI. My question is:
Is there some simple way to do this via JavaScript? It seems like with JavaScript, I shoudl be able to:
fetch the contents of blah.html [not sure how to do this ste[
call a document.write on it, to write it into the document
probably handle some stuff dealing with escaping strings
Question:
How do I do this?
Thanks!
It's not possible, as a security feature. This post here is a discussion on the topic - Includes without local server?. As the answers say, your best best is to install a small web server on the machine if you can. They're not too hard to get going.
I have used nginx before with good results. http://en.wikipedia.org/wiki/Nginx
HTML5Rocks has a tutorial on how to read local files using HTML5's File API:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
Outside this, Javascript generally does not have the ability to access local filesystems.
Update - I assumed the main file was on a server, and you wanted that file to access local files. On re-reading, it appears all your files are local, in which case, some of the answers below will work.
So guys I am working on a program to like download stuff using javascript. I hav written the following code:
function download()
{
alert("Hello");
var url='http://somesite/somefile.rar';
window.open(url,'Download');
}
The code is pretty easy, but is there some other way to download the file using javascript? Also having downloaded the file is there some way to store it locally in the location of our choice, like d-drive? I had come across the javascript file api while searching the web, is it any useful in my scenario? Please help.
Edit: Fixed code formatting
No, this isn't possible. It is up to the client where to save files, not you.
Through Javascript, you can only set the file name that you want, but would not be able to access the file system i.e. All the folders, etc..