Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:20:49+00:00 2026-06-10T20:20:49+00:00

I have an HTML form and I am using Python to generate a log

  • 0

I have an HTML form and I am using Python to generate a log file based on the input. I’d like to also be able to allow the user to upload an image if they choose. I can figure out how to manipulate it with Python once it’s there, but I’m not sure how to get the image uploaded. This has most certainly been done before, but I’m having a hard time finding any examples. Can any of you point me in the right direction?

Basically, I’m using cgi.FieldStorage and csv.writer to make the log. I want to get an image from the user’s computer and then save it to a directory on my server. I will then rename it and append the title to the CSV file.

I know there are a lot of options for this. I just don’t know what they are. If anyone could direct me toward some resources I would be very appreciative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:20:51+00:00Added an answer on June 10, 2026 at 8:20 pm

    Since you said that your specific application is for use with the python cgi module, a quick google turns up plenty of examples. Here is the first one:

    Minimal http upload cgi (Python recipe) (snip)

    def save_uploaded_file (form_field, upload_dir):
        """This saves a file uploaded by an HTML form.
           The form_field is the name of the file input field from the form.
           For example, the following form_field would be "file_1":
               <input name="file_1" type="file">
           The upload_dir is the directory where the file will be written.
           If no file was uploaded or if the field does not exist then
           this does nothing.
        """
        form = cgi.FieldStorage()
        if not form.has_key(form_field): return
        fileitem = form[form_field]
        if not fileitem.file: return
        fout = file (os.path.join(upload_dir, fileitem.filename), 'wb')
        while 1:
            chunk = fileitem.file.read(100000)
            if not chunk: break
            fout.write (chunk)
        fout.close()
    

    This code will grab the file input field, which will be a file-like object. Then it will read it, chunk by chunk, into an output file.

    Update 04/12/15: Per comments, I have added in the updates to this old activestate snippet:

    import shutil
    
    def save_uploaded_file (form_field, upload_dir):
        form = cgi.FieldStorage()
        if not form.has_key(form_field): return
        fileitem = form[form_field]
        if not fileitem.file: return
    
        outpath = os.path.join(upload_dir, fileitem.filename)
    
        with open(outpath, 'wb') as fout:
            shutil.copyfileobj(fileitem.file, fout, 100000)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ASP MVC 3 application, I have this form @using (Html.BeginForm()) { <input
I have an ASP.NET MVC form laid out something like this: @using (Html.BeginForm(null, null,
I have a webpage with a form, which looks kinda like this: @using (Html.BeginForm(MyAction,
i have html form i am using <a href="#" onclick="document.aa.submit()"> instead of submit button
I have a cancel button in a form: @using (Html.BeginForm(ConfirmBid,Auction)) { some stuff ...
I am creating one form in html using table. Which have one Drop down
I have the following form: @model Teesa.Models.SearchModel @using (Html.BeginForm(Index, Search, FormMethod.Get, new { id
I have a following problem, I have HTML form that uploads a file with
My input tag started to go crazy... I have html form for input to
I have a html form that posts to a python script test.py. If someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.