当前位置: 首页 > 面试题库 >

从jQuery请求数据中获取一个空的ImmutableMultiDict对象

云鸿祯
2023-03-14
问题内容

尝试将文件上传到服务器,但ImmutableMultiDict对象返回空。

upload.html

<html>
 <head>
  <title>Upload File Ajax</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    $('#upload-file-btn').click(function() {
        var form_data = new FormData($('#upload-file')[0]);
        form_data.append('file', $('input[type=file]')[0].files[0]);
        console.log(form_data)
        $.ajax({
            url: '/uploadajax',
            data: form_data,
            type: 'POST',
            contentType: false,
            cache: false,
            processData: false,
            async: false,
            success: function(data) {
                console.log('Success!');
                // console.log(data);
            },
        });
    });
});

</script>

 </head>
 <body>
<form action="" name="upload-file" id="upload-file" method="post" enctype="multipart/form-data">
    <fieldset>
        <label for="file">Select a file</label>
        <input name="file" id="file" type="file">
    </fieldset>
    <fieldset>
        <button id="upload-file-btn" type="button">Upload</button>
    </fieldset>
</form>

 </body>
</html>

烧瓶

app = Flask(__name__)

@app.route('/')
def index():
  return render_template("upload.html")


@app.route('/uploadajax', methods = ['POST'])
def upldfile():
  logging.debug('request.method : %s',  request.method)
  logging.debug('request.files : %s', request.files)
  logging.debug('request.args : %s', request.args)
  logging.debug('request.form : %s', request.form)
  logging.debug('request.values : %s', request.values)
  logging.debug('request.headers : %s', request.headers
  logging.debug('request.data : %s', request.data)

  upload_files = request.files.getlist("file")

  logging.debug('upload_files : %s', upload_files)

  file_val = request.files

  for k, v in file_val.items() :logging.debug('key : %s -- value : %s ', k, v)


  # if request.method == 'POST':file_val = request.files['file']
  return render_template('upload.html')


if __name__ == '__main__': 
  app.run(host='127.0.0.1', debug=True, port=2345, use_reloader=True)

这是日志数据

    016-05-03 00:05:30,500 - root - DEBUG - request.method : POST
    2016-05-03 00:05:30,500 - root - DEBUG - request.files : ImmutableMultiDict([])
    2016-05-03 00:05:30,500 - root - DEBUG - request.args : ImmutableMultiDict([])
    2016-05-03 00:05:30,501 - root - DEBUG - request.form : ImmutableMultiDict([])
    2016-05-03 00:05:30,501 - root - DEBUG - request.values : CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([])])
    2016-05-03 00:05:30,501 - root - DEBUG - request.headers : Referer: http://127.0.0.1:2345/
    Origin: http://127.0.0.1:2345
    Content-Length: 368
    User-Agent: Mozilla/5.0....
    Connection: keep-alive
    X-Requested-With: XMLHttpRequest
    Host: 127.0.0.1:2345
    Accept: */*
    Accept-Language: en-US,en;q=0.8
    Content-Type: false
    Accept-Encoding: gzip, deflate2016-05-03 03:13:58,236 - root - DEBUG - request.data : ------WebKitFormBoundaryA8jmfBTRKGJFtMS
Content-Disposition: form-data; name="file"; filename="test_upload.txt"
Content-Type: text/plain

qbcdefgh
abcdefgh
------WebKitFormBoundaryA8jmfBTRKGJFtMS
Content-Disposition: form-data; name="file"; filename="test_upload.txt"
Content-Type: text/plain

qbcdefgh
abcdefgh
------WebKitFormBoundaryA8jmfBTRKGJFtMS--


2016-05-03 00:05:30,501 - root - DEBUG - upload_files : []

问题答案:

您的jQuery版本太旧,无法引起此情况。

当我使用 2.1.1时 ,代码运行正常。

顺便说一句,您不需要form_data.append



 类似资料:
  • 问题内容: 这是我在index.html上的代码: 我如何编程test.php以获取在AJAX API中发送的“数据”? 问题答案: 您在这里问一个非常基本的问题。您首先应该阅读一些Ajax教程。只是为了帮助您(假设您知道发送数据的GET和POST方法),数据中的“数据”:函数(数据)中的“数据”与“数据”不同。为了清楚起见,您应该为它们命名不同,如下所示: 这清楚地表明,一个是要通过POST参数

  • 我从jquery发送如下请求: 在servlet内部,我可以获得参数,但问题是参数包含序列化的表单数据:如下所示: 我知道我可以使用来获取数据,但我想确保--如果他们的方法简单与否?

  • 原理 对于POST请求的处理,koa2没有封装获取参数的方法,需要通过解析上下文context中的原生node.js请求对象req,将POST表单数据解析成query string(例如:a=1&b=2&c=3),再将query string 解析成JSON格式(例如:{"a":"1", "b":"2", "c":"3"}) 注意:ctx.request是context经过封装的请求对象,ctx.

  • 使用方法 在koa中,获取GET请求数据源头是koa中request对象中的query方法或querystring方法,query返回是格式化好的参数对象,querystring返回的是请求字符串,由于ctx对request的API有直接引用的方式,所以获取GET请求数据有两个途径。 1.是从上下文中直接获取 请求对象ctx.query,返回如 { a:1, b:2 } 请求字符串 ctx.que

  • 问题内容: 基本上,我在这里尝试做的是检索图像并将其转换为base64。 通过阅读此处的评论,它指出: “当然。在将资源作为ArrayBuffer提取后,从中创建一个blob。一旦有了它,就可以直接对base / blob 或进行base64编码 。” 但是,只是,而我需要从图像中获取二进制文件,以便可以将其转换为base64并使用数据将其显示在img标签中。 有人知道如何实现这一目标吗? 先感谢

  • 问题内容: 我正在尝试让我的函数返回http get请求,但是,无论如何,它似乎在?scope中丢失了。我对Node.js不熟悉,因此不胜感激 问题答案: 当然,您的日志会返回:您在完成请求之前先进行日志。问题不是范围,而是 异步性 。 是异步的,这就是为什么它将回调作为参数的原因。做您在回调中要做的事情(传递给的):