我是生物信息学专业的学生,我从html脚本上传文件时遇到问题。我尝试使用我在互联网上找到的不同模板,但我无法解决我在本网站上搜索其他主题的问题。
这是我上传文件的html表单脚本的一个片段:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<BR>
<CENTER><FORM ACTION="blast_parser.cgi" METHOD="POST" ENCTYPE="multipart/form-data">
<FONT SIZE=+1>Upload your sequence(s) file</FONT>
<BR><INPUT TYPE="file" NAME= "file" ID="file">
<BR>
<BR>
<INPUT TYPE="submit" VALUE="BLAST!" NAME="submit" ID="submit">
<input type="reset" value="Clear Form"><br>
<BR>
<BR>
这是我用来将文件上传到服务器的 blast_parser.cgi 的一部分:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cgi, os
import cgitb; cgitb.enable()
import glob, subprocess, shlex, sys, re
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
def fbuffer(f, chunk_size=10000):
while True:
chunk = f.read(chunk_size)
if not chunk: break
yield chunk
form = cgi.FieldStorage()
fileitem = form['file']
if fileitem.filename:
fn = os.path.basename(fileitem.filename)
f = open('/opt/lampp/htdocs/server_v1/uploads/' + fn, 'wb', 10000)
for chunk in fbuffer(fileitem.file):
f.write(chunk)
f.close()
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
因此输出为:'没有上传文件'。问题是测试如果fileitem.filename:呈现无,因为 fileitem 是MiniFieldStorage('file','Acinetobacter_pittii_protein。 faa')当我上传名为Acinetobacter_pittii_protein.faa的文件时
我正在使用Linux的 Ubuntu 16.04 LTS 和 XAMPP 7.1.10 。
我只想将文件上传到服务器,以便我可以处理它。我不知道你是否需要剩下的代码,我没有把它全部放在一边,因为它们很长。
我真的很感激任何帮助! :)
答案 0 :(得分:1)
在Python中确实很容易。
有关完全包含的示例,请参阅下面的示例脚本(python3)。代码由注释记录。如果您需要进一步说明,请询问:)
#!/usr/bin/python3
# Import Basic OS functions
import os
# Import modules for CGI handling
import cgi, cgitb, jinja2
import urllib.request
# enable debugging
cgitb.enable()
# print content type
print("Content-type:text/html\r\n\r\n")
# HTML INPUT FORM
HTML = """
<html>
<head>
<title></title>
</head>
<body>
<h1>Upload File</h1>
<form action="sendEx1.py" method="POST" enctype="multipart/form-data">
File: <input name="file" type="file">
<input name="submit" type="submit">
</form>
{% if filedata %}
<blockquote>
{{filedata}}
</blockquote>
{% endif %}
</body>
</html>
"""
inFileData = None
form = cgi.FieldStorage()
UPLOAD_DIR='uploads'
# IF A FILE WAS UPLOADED (name=file) we can find it here.
if "file" in form:
form_file = form['file']
# form_file is now a file object in python
if form_file.filename:
uploaded_file_path = os.path.join(UPLOAD_DIR, os.path.basename(form_file.filename))
with open(uploaded_file_path, 'wb') as fout:
# read the file in chunks as long as there is data
while True:
chunk = form_file.file.read(100000)
if not chunk:
break
# write the file content on a file on the hdd
fout.write(chunk)
# load the written file to display it
with open(uploaded_file_path, 'r') as fin:
inFileData = ""
for line in fin:
inFileData += line
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
print(jinja2.Environment().from_string(HTML).render(filedata=inFileData))