Is it possible to manipulate a file only with client side programming? I am creating a text editor, which takes an uploaded file and need to save to another location after editing. Is it possible with client side coding?
Yes, everything you described is possible. Things you need are: the FileReader API for reading the files and download attribute of a for saving the with specific name. The solution won't be cross-browser but it'll work in the modern browsers.
Edit 1: You can't to the manipulation like in language which have full access to the file system (i.e. you can't open files by their path for example). The user needs to drag the text file in the page which is your editor or select the file with <input type="file" />.
Edit 2: The same is for saving the file. You can give the user a link with specified file name in the download attribute. When the user press this link the file will be saved in his download folder.
Related
I have a web application that receives a simple text file, but I need this file to be downloaded to a specific path. Meaning, when the application receives a text file, it will always be downloaded to a specific folder (for example, to C:\MyFolder). If it isn't possible, then I need to copy the file from where user has chosen to my folder.
This application is based on JavaScript.
JavaScript cannot exert any control over my (the visitor's) local filesystem. I remain in complete control of where my downloaded files go, what they are named, and indeed whether I even want to download them in the first place.
Sorry, but the best you can do is inform your users where to put the file you're offering for download. You cannot use JavaScript to choose the destination yourself.
You should be able to do this using a Java applet assuming that you have signed it. The user would be asked to allow your code to run and if allowed, you could do whatever you want: Including downloading a file to a specific location.
I have a web application that receives a simple text file, but I need this file to be downloaded to a specific path. Meaning, when the application receives a text file, it will always be downloaded to a specific folder (for example, to C:\MyFolder). If it isn't possible, then I need to copy the file from where user has chosen to my folder.
This application is based on JavaScript.
JavaScript cannot exert any control over my (the visitor's) local filesystem. I remain in complete control of where my downloaded files go, what they are named, and indeed whether I even want to download them in the first place.
Sorry, but the best you can do is inform your users where to put the file you're offering for download. You cannot use JavaScript to choose the destination yourself.
You should be able to do this using a Java applet assuming that you have signed it. The user would be asked to allow your code to run and if allowed, you could do whatever you want: Including downloading a file to a specific location.
How can I allow a user to just type or paste in the file that they want?
By clicking Choose File above I must use the file selector that pops up. How can I make it so I can just type in test.png?
Short answer is you can't.
This is a security measure. You are not allowed to browse the client's local file directory and find a file - they have to provide it for you. This even includes the user pasting the entire file path. You won't be able to make any meaning of it in JS - the file has to come through the native file browser or a drag-and-drop operation.
In an html page I have input type='text' and a button
filename: [ ]
[PUSH]
The user goes and fills the filename with "C:\test.txt"
When the button is pushed, (onclick), I want to write some javascript to upload the file
(e.g.: c:\test.txt) and submit to the server.
Obviously the input type file would be hidden and programmatically edited.
is this doable or are there any limitations ?
The idea is that the user does not have to go and use the input type file and always go to the open dialog
You cannot manipulate the filename before upload. You must do this on the server. This is a security measure to prevent the browser from being able to upload arbitrary files.
Your run of the mill javascript does not have access to the client's file system, and this is a good thing. Use the type="file" input that interfaces with the user's OS instead.
As far as I understand your question you want to enable to select filename from local disk and upload it without user selecting it first?
Then the answer is no. You cannot do that for security reasons, JS have no access to your local disks.
Also, you can't upload files via JavaScript, you need to have some server-side code to do that as well.
For security reason you can not do this with JavaScript. But there exists a number of other upload tools (ex Java Applets, ActiveX or Flash) web components that could "theoretically" do this.
I suggest that your search around for "File transfer java applet".
There is a Blog article that explains and compares some of those web upload components. Take a look at http://www.filecatalyst.com/comparison-of-web-based-file-transfer-methodologies
I generate html pages which are viewed locally. Is there a way i can use javascript to save files in a user designated folder? (like C:/dev/myapp/here)
I looked at HTML5 File API on MDN and see how to read files when a user selects them. No way to save files. I know how i can cache files by creating image tags but i like to generate a single button which will kick off a script to download a series of files (zip, gif, png, jpg). I dont suppose i can do that with javascript can i?
I dont suppose i can do that with javascript can i?
Nope, you cannot.