我希望将多个文件上传到Bottle服务器.
单个文件上传效果很好,通过将HTML输入标记修改为"多个",浏览按钮允许选择多个文件.上传请求处理程序仅加载最后一个文件.如何一次性上传所有文件?
我正在尝试的代码:
from bottle import route, request, run
import os
@route('/fileselect')
def fileselect():
return '''
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category" />
Select a file: <input type="file" name="upload" multiple />
<input type="submit" value="Start upload" />
</form>
'''
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
print dir(upload)
name, ext = os.path.splitext(upload.filename)
if ext not in ('.png','.jpg','.jpeg'):
return 'File extension not allowed.'
#save_path = get_save_path_for_category(category)
save_path = "/home/user/bottlefiles"
upload.save(save_path) # appends upload.filename automatically
return 'OK'
run(host='localhost', …
Run Code Online (Sandbox Code Playgroud) 我希望将上传文件的内容读入Javascript变量中。
该程序曾经使用file.getAsBinary进行工作,但是现在不建议使用,需要更新以使用FileReader()
表单具有文件上传选择,onsubmit在javascript中运行函数uploadDB()。
可以很好地传递变量dbname,就像上传选择中的文件名一样,我可以通过alert或console.log看到它。
接收文件文本的最终bfile变量未定义。我已经尝试了readAsText和ReadAsBinary,但是bfile是未定义的。var声明位于函数uploadDB()中,并且对于内部函数应该是全局的。如果范围是某种问题,我如何在bfile变量中得到结果。
它可能很简单,但我无法使其正常工作。有人可以建议点什么。html部分是;
<form onsubmit="uploadDB()"; method="post">
Database <input type=text name="dbName" id="dbName" size=20>
<input type=file id="metaDescFile" size=50 >
<input type=submit value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
js脚本类似于(已删除多余的内容);
<script language="javascript" type="text/javascript">
<!--
var uploadDB = function() {
var input = document.getElementById('metaDescFile');
var fname = document.getElementById('metaDescFile').value;
var file=input.files[0];
var bfile;
reader = new FileReader();
reader.onload = function(e) { bfile = e.target.result }
reader.readAsText(file);
alert(bfile); // this shows bfile as undefined
// other stuff
}
Run Code Online (Sandbox Code Playgroud)