javascript - 在 Python Flask 服务器上使用 FileDrop.js 上传文件

标签 javascript python flask filedrop.js

我运行一个 Python Flask应用程序并希望实现将文件上传到服务器的可能性。 FileDrop.js这项任务看起来很有希望,但是,我无法将其与 Flask 一起使用。

这样做的原因似乎是 Flask 期望通过 POST 将文件连同用于从应用程序识别文件的附加参数一起发送到服务器。我使用另一个文件上传框架来完成这项工作,jQuery filedrop :

<html>
<head>
<script type="text/javascript" src="https://github.com/weixiyen/jquery-filedrop/blob/master/jquery.filedrop.js"></script>
</head>
<body>

<fieldset id="zone">
    <br><br>
    <legend><strong>Drop one or more files inside...</strong></legend>
    <br><br>
</fieldset> 

        <script type="text/JavaScript">

            // when the whole document has loaded, call the init function
            $(document).ready(init);

            function init() {

                var zone = $('#zone'),
                    message = $('.message', zone);

                // send all dropped files to /upload on the server via POST
                zone.filedrop({
                    paramname: 'file',
                    maxfiles: 200,
                    maxfilesize: 20,
                    url: '/upload',
                }

            }

        </script>

</body>
</html>

paramname: 'file' 以某种方式随请求一起发送,以便在我的 Flask 应用程序中,我可以通过以下方式获取上传的文件:

@app.route('/upload', methods=['POST'])
def upload():

    if request.method == 'POST':
        file = request.files['file']
        file.save('myfile.ext')
        return 'Done'

但是,如何使用 FileDrop.js 获取我上传的文件? ?我在文档中看不到如何通过 POST 传递附加参数的可能性。例如,当我遵循文档中的最小示例时

<html>
<head>
<script type="text/javascript" src="https://github.com/ProgerXP/FileDrop/blob/master/filedrop.js"></script>
</head>
<body>

    <fieldset id="zone">
      <legend>Drop a file inside...</legend>
      <p>Or click here to <em>Browse</em>...</p>
    </fieldset>

        <script type="text/JavaScript">

            // when the whole document has loaded, call the init function
            $(document).ready(init);

            function init() {

                var options = {iframe: {url: '/upload'}}
                var zone = new FileDrop('zone')

                // Do something when a user chooses or drops a file:
                zone.event('send', function (files) {

                // FileList might contain multiple items.
                files.each(function (file) {

                // Send the file:
                file.sendTo('/upload')

                  })
                })

            }

        </script>

</body>
</html>

然后尝试在 Flask 中检查上传的文件:

@app.route('/uploadtest', methods=['POST'])
def uploadtest():

    print(request.files)

    return 'end'

request.files 现在是一个 ImmutableMultiDict([]) 我不知道如何从 Flask 访问它。有什么建议吗?

最佳答案

我实际上正在处理同样的问题,也许可以帮助您解决我所确定的问题。

首先,flask 请求中的 files 属性仅在数据从使用特定编码和类型的表单发送时有效。 FileDrop 不使用它,因此请求中的 files 属性应该为空。

A tag is marked with enctype=multipart/form-data and an is placed in that form. http://flask.pocoo.org/docs/0.10/patterns/fileuploads/

FileDrop 似乎不像 dropzone.js 使用这种形式那样发送它。我所做的是查看 FileDrop 发出的网络请求。这是一个职位。我可以看到它是作为帖子处理的。数据在哪里?在我浏览器的网络请求中,我可以看到一个名为 X-File-Name 的 header ,它是正在上传的文件的 url 引用名称。我可以在请求对象中访问它。

fileName = urlparse.unquote(request.headers['X-File-Name'])

实际数据在哪里?它位于 POST 请求的正文中。这是在 request.data 中——无论上传时采用何种编码格式的整个文件。

foo = open('/tmp/%s' % fileName, 'w')
foo.write(request.data)
foo.close()

这只是一个示例,但它确实有效。显然,您仍然应该遵循我为保护该文件名而链接的 flask “文件上传”页面上的安全提示,但除此之外,仅此而已。

使用帖子主体的直接缺点是每个请求一个文件,但这对我的特定应用程序来说并不是什么大问题。希望有所帮助。

关于javascript - 在 Python Flask 服务器上使用 FileDrop.js 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574525/

相关文章:

Python 单元测试 : No tests discovered in Visual Studio Code

flask - 如何在支持的 SQL alchemy 中存储参数,这些参数将传递给 Flask 应用程序中的 celery 任务

python - Flask 应用程序在某些目录中无法运行

python - 控制 Flask 应用程序中的 URL 顺序

javascript - 在 javascript 中每次 onclick 和循环后追加数组

javascript - 使用 PhoneGap 下载文件并在 AngularJS 中使用

javascript - 在 React 中更改渲染映射数组的排序键

python - urllib3 HTTPResponse.read() 返回空字节

javascript - 模拟点击加载在移动设备上不起作用

python - 将具有分层列索引的宽格式 pandas DataFrame 转换为整齐格式