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.
Related
I am new in web development and what I am trying to achieve is as follows:
On a page where user create a blog
User upload image to a CDN via ajax and page will show image after
ajax success.
The uploaded image to CDN has a new file name and obviously URL address but the filename in input type="file" remains to be the original file name. During form submission, the ajax-returned filename has to be submitted for server side processing.
I am not sure if I am doing it the right way as how I would normally observe on many websites. My question is, how do people normally sync filenames in above case when an Ajax upload is used and the file name has been changed and stored in CDN prior to a final form submission?
I am using Django but I guess that doesn't really make a huge difference.
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 access url of file which user select through pop up file directory navigation window. My browse button tag is:
<input type="file" id="loadFile"/>
On the back end, i can access the file url in javascript, but not sure how to do it in PHP.
You have to have the correct enctype for the form.
Otherwise, you utilize the $_FILES super global.
This is covered extensively in the PHP Manual regarding uploads.
The original filename is available in $_FILES['load file']['name']
Since it seems that you actually want a way to have the user provide a url to a file, the way to handle that is to simply implement a text input and accept the url there, and process the url on the server, using an HTTP client that fetches and stores the file on the user's behalf.
For years people have been using the curl extension, which is fast and highly functional. There are also a number of client libraries written in php like Guzzle.
Is there a way to set the accepted file types in a django forms.FileInput() widget? I want it to accept only .txt and .pdf.
The docs, contain nothing. If this is not at all possible, how can we add the validation at client side for a django form?
You should ideally do this when cleaning your form data and return an error if its not the extension type you want.
But there is a django snippet that shows an example of how to do this using a custom form field: http://djangosnippets.org/snippets/977/
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?