Osc*_*ani 1 javascript python html5 cgi
客户端有一个简单的表单,带有文本和文件:
<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>
Run Code Online (Sandbox Code Playgroud)
使用Javascript我将数据发送到服务器的Python CGI脚本:
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);
}
Run Code Online (Sandbox Code Playgroud)
在服务器端Python CGI脚本中我有字段存储fs = cgi.FieldStorage(),我知道如何获取文本值,即fs['key'].value.
如何保存上传到磁盘的文件?
我希望我足够清楚.谢谢!
使用此代码将文件存储在磁盘上
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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4498 次 |
| 最近记录: |