I'm looking for a way to let user upload a config file (text base, no encryption), then parsing that file (either line by line or separating fields by commas,...) to pre-fill textboxes field on the same page using javascript.
Thanks for your help :)
First, you'll have to handle the upload via a ajax upload. Your javascript then can make an ajax call out to get the contents of the HTML file. What exactly are your need beyond this?
Related
I'm looking for a way to add a string as a file inside a html form using JavaScript.
Now I know about the security issues the browsers have when it comes to files, but I'm not looking for adding a file path (as happens when you click the browse of a file input)!!
I want to add a string as a file (The server I'm hitting expects the post to include a file and not a simple form entry).
I know how to perform the action using the FormData object, but I don't want to generate a xhr(ajax) post. I need the post to be a simple form post since the server responds with a redirection.
Does anyone have an idea about this?
I want to make a web site where you can choose a local file (XML/JSON) and then proceed to a Django view that will read data out of it. Should I use javascript for choosing form and to send file to specific URL (for Django view)? How to do that? Any examples?
I started from here.
Whether you use javascript or not, the Django aspect will still be the same. You'll have a django view that handles a post with your file. Assuming you are using <input type="file" name="" />, you can access your file through request.FILES. See the documentation here.
A quick example of what your view would do:
def view_handling_file(request):
file_name = request.FILES['input_name'].name #input_name refers to the name attr in your file input
file_data = request.FILES['input_name'].read()
# do stuff with your file
With the page that allows your users to select a file, you can either have it just submit. Or you can use javascript to make the post request via ajax. You may need to use a jquery plugin like jquery-iframe-transport to pass files via ajax. It'll be easier to just have your form submit.
I have a form that includes a file upload.
I need to extract some meta information using javascript from the file before saving the contents of the form to my database.
What would be the best way, if any, of achieving this?
To clarify: The issue isn't the with extracting the file meta but rather how to access the $_FILES array and execute a js file with this data before finally allowing the form to submit to the server.
How I am achieving this currently:
I submit the form to the controller action
It saves the data and then it sets a view variable with the location of
the uploaded file before re-rendering the initial view upon successful form submission
The view checks if the variable is set, and if it is, executes the
javascript which basically extracts the meta now that it can access where the file is, and updates the db
with an ajax call.
The above method is not ideal in that the meta belongs to the same row of data that is saved when the form first submits so ideally I would like to include it initial form submission and not when the page is rendered again after the form has been submitted. Seems like a bit of a dirty hack to me but I can't as of yet see another way this can be achieved.
I would suggest uploading using ajax (SWFUpload/Uploadify) and then sending data back to the view with the processed file data.
You can upload the file, issue a number of callbacks, and finally return the rendered filedata back to the waiting javascript function that issued the request.
Just my two cents
What sort of metadata? To answer your question, yes, this is possible, but only if the browser supports the File API. This excludes IE9 and older. For more information, see this MDN article on FileReader. There are various libraries that will extract specific types of metadata from files. For example, if you are interested in parsing EXIF metadata from an image file, you should look into something like jQuery-fileExif.
I need to create a CSV File editor. The user should be able to upload a CSV file, and the data should automatically be loaded into a text area. I have seen many examples of CSV data being transferred from a text area to, for example, a table, but I can't find something that will help me with actually uploading a file, reading the data from it in to a 2D array, and then placing it in a text area.
I can transfer the data into an editable table from the text area without problem.
So the question is: How can I upload a file, read the data from it (and keep the format of the CSV) and display the correct format in a text area element?
I am not currently concerned about checking to see if the CSV is correct etc etc. I just need to know how to handle the data. Sorry, but my Ajax knowledge is very limited. Any help or guidance would be appreciated.
Uploading it is no different than how any other form works in HTML.
You can use a file input to select a file, then upload it as part of the form data (it's automatic) or supply the user with a textarea to paste into. Submitting the form will "upload" the data to the server. You don't need AJAX to do any of this, but it is an option.
What you do with the data on the server is up to you.
I have a requirement where i need to store the data selected by user into the txt file. How can i do that? My form is in html and javascript.
The only way I know how is with php. Any other server side language would work.
The following tutorial has everything you need to know about working with files using php.
http://www.tizag.com/phpT/files.php
create file: http://www.tizag.com/phpT/filecreate.php
write to file: http://www.tizag.com/phpT/filewrite.php
be careful here. You need to choose the correct method of writing to the file depending on weather you want to add to what is already in the file or override it. To append see this: http://www.tizag.com/phpT/fileappend.php