是否可以获取使用 dropzone.js 上传的文件的文件夹名称
Is it possible to get the foldername for a file uploaded with dropzone.js
我正在使用 dropzone.js 作为我的 Flask 应用程序的一部分,用户可以在其中同时上传多个文件夹。但有时有关文件的最有价值的信息是文件夹名称。
例如:
文件夹 A
- sample.txt
- img.png
文件夹 B
- sample.txt
- img.png
这里是否可以注册哪些文件来自文件夹 A,哪些文件来自文件夹 B,假设他们将整个文件夹拖放到拖放区中?
在 and MDN's article on Using FormData objects 的帮助下,我想出了如何做到这一点。
如果您在模板中像这样定义放置区:
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("form#my-awesome-dropzone",
{ url: "/upload",
});
然后您必须为 sending
事件添加事件处理程序,该事件采用 file.fullPath
字符串,并将其添加到成为 multipart/form-data
提交的一部分的 blob。
myDropzone.on("sending", function(file, xhr, formData) {
// get the original fullPath and add it to the multipart form.
var blob = new Blob([file.fullPath], { type: "text/plain"});
formData.append("originalPath", blob);
});
请注意,这实质上是将名为 blob
且具有 text/plain
mimetype 的第二个文件添加到您的表单提交中。为了可视化这一点,在您的 Flask 服务器上的上传路径中,您可以 print(request.files)
给出:
ImmutableMultiDict(
[('originalPath', <FileStorage: 'blob' ('text/plain')>),
('file', <FileStorage: 'up.txt' ('text/plain')>)
])
因此,要从服务器端的字符串中提取实际字符串,您可以这样做:
original_path = request.files['originalPath'].read().decode('utf-8')
请注意,从前端的角度来看,dropzone 不知道相对于被删除的文件或文件夹的父文件夹,因此:
- 将名为
up.txt
的文件放入 Dropzone,original_path
将是 'undefined'
。
- 将包含名为
up.txt
的文件的名为 untitled folder
的文件夹放入 Dropzone,original_path
将是 'untitled folder/up.txt'
。
您应该在后端构建一些功能来验证传入的信息,并相应地解析这些字符串。
我正在使用 dropzone.js 作为我的 Flask 应用程序的一部分,用户可以在其中同时上传多个文件夹。但有时有关文件的最有价值的信息是文件夹名称。
例如:
文件夹 A
- sample.txt
- img.png
文件夹 B
- sample.txt
- img.png
这里是否可以注册哪些文件来自文件夹 A,哪些文件来自文件夹 B,假设他们将整个文件夹拖放到拖放区中?
在
如果您在模板中像这样定义放置区:
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("form#my-awesome-dropzone",
{ url: "/upload",
});
然后您必须为 sending
事件添加事件处理程序,该事件采用 file.fullPath
字符串,并将其添加到成为 multipart/form-data
提交的一部分的 blob。
myDropzone.on("sending", function(file, xhr, formData) {
// get the original fullPath and add it to the multipart form.
var blob = new Blob([file.fullPath], { type: "text/plain"});
formData.append("originalPath", blob);
});
请注意,这实质上是将名为 blob
且具有 text/plain
mimetype 的第二个文件添加到您的表单提交中。为了可视化这一点,在您的 Flask 服务器上的上传路径中,您可以 print(request.files)
给出:
ImmutableMultiDict(
[('originalPath', <FileStorage: 'blob' ('text/plain')>),
('file', <FileStorage: 'up.txt' ('text/plain')>)
])
因此,要从服务器端的字符串中提取实际字符串,您可以这样做:
original_path = request.files['originalPath'].read().decode('utf-8')
请注意,从前端的角度来看,dropzone 不知道相对于被删除的文件或文件夹的父文件夹,因此:
- 将名为
up.txt
的文件放入 Dropzone,original_path
将是'undefined'
。 - 将包含名为
up.txt
的文件的名为untitled folder
的文件夹放入 Dropzone,original_path
将是'untitled folder/up.txt'
。
您应该在后端构建一些功能来验证传入的信息,并相应地解析这些字符串。