javascript - In my Python CGI script, how do I save to disk a file uploaded via POST request of data entered by the user in a fo

The client has a simple form that takes a text and a file:<form name="add_show" id="a

The client has a simple form that takes a text and a file:

<form name="add_show" id="add_show" action="" method="GET">

    <label class="text-info">Show Name:</label>
    <input type="text" id="show_name" name="show_name" placeholder="My Show Name" required><br><br>

    <label class="text-info">Show's File (JSON):</label>
    <input type="file" id="file" name="file" required><br><br>

    <p><input class="btn btn-danger btn-small" name="button2" value="Add the Show!" onClick="addFullShow(this.form)"></p>

</form>

With Javascript I send the data to the server's Python CGI script:

function addFullShow(form) {

      alert("about to send form");

      var formElement = form;
      formData = new FormData(formElement);

      var xhr = new XMLHttpRequest();
      xhr.open("POST", "myScript.cgi");
      xhr.send(formData);

    }

And in the server side Python CGI script I have the field storage fs = cgi.FieldStorage(), and I know how to get the text values, i.e. fs['key'].value.

How do I save the file uploaded to disk?

I hope I'm clear enough. Thanks!

The client has a simple form that takes a text and a file:

<form name="add_show" id="add_show" action="" method="GET">

    <label class="text-info">Show Name:</label>
    <input type="text" id="show_name" name="show_name" placeholder="My Show Name" required><br><br>

    <label class="text-info">Show's File (JSON):</label>
    <input type="file" id="file" name="file" required><br><br>

    <p><input class="btn btn-danger btn-small" name="button2" value="Add the Show!" onClick="addFullShow(this.form)"></p>

</form>

With Javascript I send the data to the server's Python CGI script:

function addFullShow(form) {

      alert("about to send form");

      var formElement = form;
      formData = new FormData(formElement);

      var xhr = new XMLHttpRequest();
      xhr.open("POST", "myScript.cgi");
      xhr.send(formData);

    }

And in the server side Python CGI script I have the field storage fs = cgi.FieldStorage(), and I know how to get the text values, i.e. fs['key'].value.

How do I save the file uploaded to disk?

I hope I'm clear enough. Thanks!

Share Improve this question asked Apr 11, 2013 at 11:47 Oscar_MarianiOscar_Mariani 7683 gold badges9 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

use this code to store the file on disk

import os, cgi
fs = cgi.FieldStorage()
fileitem = fs['userfile']

# Test if the file was uploaded
if fileitem.filename:
   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())
   message = 'The file "' + fn + '" was uploaded successfully'
else:
   message = 'No file was uploaded'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742396130a4435982.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信