Using <input type="file"> in Django - html

When someone clicks Submit after selecting a file with the <input type="file"> element, how do I access the contents of the file in Django?
(It seems like the request sent to the request handler has no trace of the file anywhere -- not even in request.FILES.)
Currently my template is like:
<form method="post" enctype="multipart/form-data">
<input type="file" enctype="multipart/form-data" name="file" accept="text/csv"/>
<input type="submit" value="Upload" />
</form>
View:
def HandleRequest(request):
print "**** request:", request
I don't see anything being printed about the file.
Note:
There's probably other ways to do this in Django, but I'm looking a solution using the simple input tag, and not something else (which would probably involve Javascript).

The code you posted, as it is posted, works fine. The HTML is sound (though I think the enctype on the <input> is redundant at best), and a very simple view shows an InMemoryFile after the POST. The problem must lie in something between the browser and your view. Some things to check:
Middleware.
Apache.
Nginx.
Decorators on your view.
mod_wsgi configuration.

Related

HTML Form Downloading Response of POST Request

I am trying to use an HTML form to execute a post request. I know that in general AJAX is probably a better way to go about this, but my use case is very simple and I have to upload a file in this form submission (which is really easy just using HTML forms). Anyways, everything works, but for some reason my browser is downloading the contents of my POST request response, which I do not want to happen. I want nothing to actually occur when I submit the post request other than the post request being sent out.
Here is the HTML portion of my code:
<form enctype="multipart/form-data" action="/action" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000"/>
<input type="text" name="email" id="email">
<input type="file" name="file" id="file" accept="image/*">
<input type="submit">
</form>
I am not sure that it is relevant, but I am using a Flask webserver and here is the response I am returning:
response = {"status": 200}
return Response(json.dumps(response), mimetype='applications/json')
The issue is that I keep having files downloaded from my browser with '{"status": 200}' in them.
Status : 200 indicates.
The request has succeeded.
So an OK status response is sent to the location, /action as you have mentioned in action="/action". So I'm guessing this is the view you wanted to send the POST request too.
I don't have the code for your /action view, but either :
This is not the view you intended to send it too.
It is the view you intended to send it too.
So if its not the correct view, what you can do is, use jinja templating and mention your view like this.
<form method="POST" action="{{url_for('xyz')}}">
Notice how I use the url_for() in jinja templating to specify route.
And if its the correct view, i can answer the question by seeing your code for the view called action.
Most likely what could be wrong though is,
your return function is erroneous. Check the return function again.

HTML input file connection was reset error

We are trying to upload files to blob storage, the process currently works, however, when trying to upload a file greater than ~28.5MB through the HTML input type="file" element, a connection was reset error message appears. When debugging, the page never hits our httppost method in the C#. However all files we have tested under 28.5MB will upload correctly.
This is the file input in our cshtml page.
<input id="files" type="file" asp-for="UploadFiles" multiple />
Our form looks like this:
<form method="post" enctype="multipart/form-data" asp-area="JobManager" asp controller="KnowledgeBase" asp-action="EditItem">
There is a limit in Kestrel noted at https://github.com/aspnet/Announcements/issues/267.

Need help sending Email from form HTML

I have a form that reads
<form action="Send.asp" method="POST" name="Order" id="Order">
in my send a quote page. The submit button reads as follows:
<input name="B1" type="submit" class="style80" value="Submit">
Now when I click on the button, it only redirects to the home page. I would like it to go to the .asp file called Send.asp
That .asp file has the sending methods: connect to SMTP server and compile an email form the input boxes on the request page.
Why is my email not sending? What am I missing from the form tag?
Your input type=submit seems correct. Is the Send.Asp page in the same directory than the html file ?. Maybe you have to use other relative route.
Are you debugging Send.asp file to check if it is being called?.
That could be a start.
Try changing aswell the Send.Asp with full url to discard that the url is not valid.
form action="http://yoururl/yourproject/Send.asp" method="POST" name="Order" id="Order">

successful file upload opens new blank page, also allows "empty/blank" to upload

following code is working but with 2 issues: It is part of bigger form on the web page it appears on.
<FORM id="my_file_upld_form"
style="display: inline;"
METHOD="POST"
ACTION="/cgi-bin/fileupload_ajax.pl"
ENCTYPE="multipart/form-data">
<INPUT TYPE="file" class="ajaxfileupload" NAME="myfile" SIZE="42">
<input type="hidden" class="ajaxfileupload" name="fileupload" value="ajax_fileupload">
<input class="ajaxfileupload" type="submit" value="Upload"/>
</FORM>
it is allowing "blank" or empty (no file selection and upload button clicked) to upload. which is causing 500 error on server.
bigger problem is: after the file is uploaded, a new blank page is opened in browser and user has to use back button to go back to the page from where this was run. This happens irrespective of the fact that the ajax script returns anything back or not.
how to fix? I am open to replace this piece of code on the page, if needed.
It's hard to tell exactly what is going wrong without your java script code. However I think I know how to fix some of your problems.
Don't trust the client you need to handle in your backend code the ability to handle a form that gives you bad data. You can validate the form client side but you always need to validate server side as well.
I suspect this problem is happening because you aren't preventing the default event in javascript. In jquery you would do
$("#my_file_upld_form").submit(function(e){
e.preventDefault()
})

Is there anyway to use the Imageshack API without PHP?

If I use the ImageShack API just in a form like this:
<form method="post" enctype="multipart/form-data" action="http://www.imageshack.us/upload_api.php">
<p><input type="file" name="fileupload"></p>
<p><input type="text" name="key" value="Your_Developer_Key"></p>
<p><input type="submit" value="Go"></p>
</form>
the browser gets taken to an XML doc, which has the image URL, but is no use because I'm no longer on my site. I've tried loading it in an iFrame and that works, but I can't access it because it's cross-domain.
A Http Post in jquery won't work because I can't send a file in it. I don't know any PHP so using that would take lots of time to learn & setup etc. Do I have any other options?
You mention you know Ruby on Rails.
Posting a file with Ruby is rather simple. See: Ruby: How to post a file via HTTP as multipart/form-data?