我有一个读取文件的表单:
<form enctype="multipart/form-data" is="iron-form" method="post" action="http://localhost:7733/receivedoc" id="restForm" >
<px-file-upload
id="uploadComponentId"
message="Drag and drop files here, or click the button below."
multiple=false
accept=".xls,.xlsx">
</px-file-upload>
<button id="saveDataSetButton">
<i class="fa-briefcase">Generate Pacing File</i>
</button>
</form>
此处px-file-upload
组件基本上只是对标准HTML5 <input type=file>
元素的重新设计,我应该能够以相同的方式使用它 - files
属性px-file-upload
包含FileList
数组,我应该能够访问并处理请求。
我试图用一些NodeJS代码发布文件:
this.$.saveDataSetButton.addEventListener('click', function() {
console.log(uploadComponentId.files[0])
console.log('restForm.saveDataSetButton click')
restForm.submit();
});
正如所料,控制台显示正在附加的文件(console.log(uploadComponentId.files[0])
的Chrome控制台输出):
文件(12233){name:“testfile.xlsx”,lastModified:1522075602000, lastModifiedDate:2018年3月26日星期一16:46:42 GMT + 0200(CEST), webkitRelativePath:“”,大小:12233,...}
但是,当请求到达服务器时,它是空的(见下文)。我正在尝试通过Flask服务器接收文件(需要使用Flask,稍后会在Excel中使用Python):
UPLOAD_FOLDER = '/Users/user/Documents/flask_min'
ALLOWED_EXTENSIONS = set(['xls', 'xlsx'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/receivedoc', methods=['POST', 'GET'])
@crossdomain(origin='*')
def upload_file():
if request.method == 'POST':
print("In there")
print(request.files)
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
return ('', 204)
服务器获取请求,但找不到该文件。服务器上的控制台输出显示为:
在那里
ImmutableMultiDict([])
http:// localhost:7733 / receivedoc'[POST]&gt;
127.0.0.1 - - [09 / May / 2018 15:15:23]“POST / receivedoc HTTP / 1.1”302 -
127.0.0.1 - - [09 / May / 2018 15:15:23]“GET / receivedoc HTTP / 1.1”204 -
显然文件部分没有到达服务器。为什么?我错过了什么?我该如何发送文件?