Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creation of a simple HTML file upload page

First, I don't need any functions more than upload a file. No progress bar, no file type, or size check, no multiple files.

What I want is the most simple HTML webpage to handle the upload and save the file with the name I specified.

I tried to use this:

<form action="../cgi-bin/upload.py" method="post" enctype="multipart/form-data">
<input type="file" name="upload" />
<input type="submit" /></form>

In upload.py:

#!/usr/bin/python

import os
import commands
import cgi, cgitb

cgitb.enable()
print "Content-Type: text/html"
print
print 'start!'
form = cgi.FieldStorage()
filedata = form['upload']

But I don't know how to save this in file, like "Beautiful.mp3".

Can any body help?

Though, really, I don't want to use any scripts. I just want the most basic html pages. Python scripts will only exist when there must be some CGI handlers. Flash is not preferred.

like image 659
iloahz Avatar asked Jun 21 '11 12:06

iloahz


People also ask

How can create upload file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do I customize the upload button in HTML?

Use a label tag and point its for attribute to the id of the default HTML file upload button. By doing this, clicking the label element in the browser toggles the default HTML file upload button (as though we clicked it directly).


1 Answers

The filedata object will wrap a file-like object that can be treated like a regular file. Basically you would do this:

if filedata.file: # field really is an upload
    with file("Beautiful.mp3", 'w') as outfile:
        outfile.write(filedata.file.read())

Or, you could do just about anything else with it, using read(), readlines() or readline()

like image 68
SingleNegationElimination Avatar answered Oct 21 '22 11:10

SingleNegationElimination