gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:50:39 24 4
gpt4 key购买 nike

我运行一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com