从HTML表单获取空文件内容

时间:2018-06-20 07:01:48

标签: javascript python jquery html flask

我正在尝试使用jquery从html表单中获取值和文件,但烧瓶服务器中接收到的文件为空。

HTML:(main.html)

<form id = "name"  enctype="multipart/form-data" align ='center' >


<p>file1: <input id = "File1" type ="file" name = "file1" required = "required"/></p>
<p>date: <input id = "Date" type ="date" name = "date" required = "required" /></p>

<p> <input type ="submit" name = "submit" /></p>

我得到日期,但是file1为空。

jQuery:

$(document).ready(function(){
    $('form').submit(function(event){

        var formdata ={
                file1 : $('#File1').val(),
                date : $('#Date').val(),
          };

        $.ajax({
        url: "/result",
        data: formdata,
        type: 'POST',
        dataType : 'json'
        })
        .done(function(data){
            console.log(data);
        });

        event.preventDefault();
    });
  });

这是烧瓶app.py:

    from flask import Flask, render_template, request, redirect, jsonify
import json

app = Flask(__name__)

@app.route('/')
def hello():
    return render_template("main.html")

@app.route('/result',methods = ['POST', 'GET'])
def result():
    if request.method == 'POST':
        print('date = ', request.form['date'])
        with open(filename, mode='wb') as localfile:     
            localfile.write(request.files['file1'].read())

        return jsonify({'name' : 'Name'})



if __name__ == '__main__':
    app.debug = True
    app.run(debug = True)

0 个答案:

没有答案