Django - Upload file without using form - django

I have a small email client. I would like to be able to upload files without having to submit a form. So, I have my email form. I would like to, whenever I use the file input button on my form, that file would be uploaded without any reload of the page. The goal is to be able to upload multiple files without a reload of the page, something similar to what happens in GMail.
Every time you click the file input and choose a file, a small progress bar appears with the upload progress, and the page is not reloaded.
I am guessing some JS/Ajax library might help me achieve this? I am using HTML5.
Thank you

Blueimp has a great jQuery-based throttling file uploader.

Related

Simple file upload to Django via HTTP request

I'm really confused with the process of uploading a file (image or pdf in my case) to a Django app programmatically (via HTTP POST or PUT request). I've read the doc but I must admit it hasn't helped me get through. The most confusing part to me is the template (and form) : why do I need to specify a template and form in the view ?
I just want to send a file to a directory, and I'd like to know what exactly is needed in order to do so on the Django part as well as on the request part (content-type... ?)
I'd be really grateful to anyone able to show me some direction here..
You don't say what doc you're reading, so it's hard for us to tell what you mean. But if you're planning on doing a programmatic upload, you don't need a template, of course. You do however need some code that accepts the POST and processes the upload: you can do that with a form, or simply access the data in request.FILES and do what you want with it yourself.
Edit It's true that that page doesn't make any reference to uploading programmatically, because most people's use cases are uploading through the browser, via a form. But the page does explain how to handle uploaded files, which is the only bit that you need.

How to use uploaded file info to fill in form fields

I am trying to create a file upload form where a user uploads a file and then I do some processing on it. After processing the file, I want to redirect the user to another form that is partially filled in already from the data I gathered from that file.
I've been reading the docs and found something on multipart forms, but don't know if this is what I should be looking into. I've found example code on uploading files and how to build forms (love Django docs!), but I'm not sure how to tie the two together.
A potential problem I've thought of is how to make sure that a user doesn't somehow skip to the other form without uploading a file. The second form should only be accessible after uploading the file and having it verified for the info I need from it.
You probably want to store the parsed information from the file in the session. That way you can check when displaying the form if the data exists in the session, and if not redirect back to the upload form. If it does exist, you can simply use it as the initial data for the form.

Download a generated file and redirect in Django

I have a form with which users submit data to my application, and the response to submitting the form is a download with data depending on what they submitted. Since the submission affects the data in the database I want to redirect from this page to prevent the submission accidentally being made twice.
The only solution I've come across is to save the file on the server and redirect to a page which causes the file to download. However I don't really want to be keeping these files or having to manage them on the server.
Is there a way to download the file and then cause the page to redirect?
Consider also the case when the user's internet connection happens to break during the download. Should the user have a possibility to request the same download again in this case? Then you need to store either the generated file or all data needed to regenerate it anyway.
presumably there are two parts to your code, one adding their data to your db, and a second generating their download. could you just do the first part on form submit, and then redirect them (perhaps including some get parameters) to a second page which reads the db and generates their download?

django admin uploaded file processing

Here's what I want to do, and I did not find something similar in my search so far.
In my admin page, I have a Filefield in my model. The rest of the fields are all read only.
I want to be able to upload a file and process it immediately and to extract info from it to assign to these read only fields.
I thought of overriding the clean_(modelfield) method for this FileField and do this parsing and assigning stuff in it. But this is not done right after the file is uploaded, right? I thought this is done when the form/entry is saved.
Next I thought of adding a custom button to this admin form called 'process' which can be clicked after the file is uploaded. This would trigger the assignment of values to the read only fields.
But I am not able to decide on what is the best approach to process the file and display the updated fields in one page without too much of tinkering.
Any thoughts? Thanks
There are two solutions that I can think of with my limited knowledge. Since, by default, the file upload will only start once the request is posted, an alternative way needs to be designed.
1. Upload file via a script and process the file: Use a script (eg: JQuery script) to upload the file and once upload is complete, trigger a script (onComplete event) to render the values into read-only field. This entire process can be associated to your "Process" button or a time-delayed trigger once the FileField is changed.
2 Custom form for file upload: You can detach the file field and other fields (read only fields that you mentioned). If you design a custom form with just the file upload field and once the user submits the request, you can render another form with rendered initial values in the read only fields. That way you need not have any script but you will have to have 2 forms.
Hope this helps. If you find any other solution, do share it :)

Prompt a confirmation page for a form with FileInput field in Django

I'm developing an admin interface (without a model) for a data restore process. The form on the page allows user to upload a restore file. Now, what I'd like to do is, when the user submit the form, I want to upload the file first, pre-process it, then show a confirmation page to user.
In the pre-process, assuming the form is valid, I unpackage the restore file and extract the backup time from it, then show the backup time (and possibly other information from the restore file) to user in the confirmation page.
My problem is I don't know what the best way is to save the file state. Ideally I'd like to pass the uploaded file to the confirmation page form then when user agrees to continue, I use the restore file to actually restore the system. However, I can't seem to figure out how to do it.
So what I did was to save the file first, then somehow include the filepath in the confirmation page in a hidden field. However, there's a security risk by doing this since the filepath can be modified when user submits the confirmation form.
What's the best way to tackle this problem?
Thanks!
Store the filepath location in the session. You can access the session from your view:
def your_view(request):
...
request.session['file_path'] = 'the file path'
Then in another view get it out:
def your_other_view(request):
...
request.session.get('file_path')
and pass it to your template for use.
You don't have to worry about security, (from the Django docs):
It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID – not the data itself.
I tend to solve this problem using a model, that keeps track of a file, and when it was last updated/uploaded. That way in your hidden input you can embed an ID you retrieve later - so if an attacker wants to mess with your form field, all they're going to get is a different backup file (and you could probably write extra validation to handle that).
I'd recommend you create a temporary model to handle your file for you, and then just delete it when you're done processing the file (at the end of the second step).