简体   繁体   English

如何自定义FancyUpload以上传多个文件,每个文件都有自己的数据(例如标题,标题)?

[英]How can I customize FancyUpload to upload multiple files, each with its own data (e.g. caption, title)?

I'll be answering this question 我会回答这个问题

I want to allow my users to upload multiple files in one go and want to use FancyUpload to do it. 我想允许我的用户一次性上传多个文件,并希望使用FancyUpload来完成。 I can get a basic version of FancyUpload working but I now need to allow users to specify some metadata for each file such as a caption or title. 我可以使用FancyUpload的基本版本,但现在需要允许用户为每个文件指定一些元数据,例如标题或标题。

Unfortunately, I've run into a couple of problems. 不幸的是,我遇到了两个问题。 First, I need to visually link the inputs for a file with that file. 首先,我需要在视觉上将文件的输入与该文件链接。 Upon choosing files for upload, FancyUpload displays a list of those files. 选择要上传的文件后,FancyUpload将显示这些文件的列表。 Therefore, I will need a differing number of inputs depending on how many files have been chosen and I will also need to associate each of those inputs with a specific file. 因此,根据选择了多少文件,我将需要不同数量的输入,并且还需要将这些输入中的每一个与特定文件相关联。

Second, I need to POST the metadata along with the file. 其次,我需要将元数据和文件一起发布。 However, FancyUpload only allows you to specify metadata for every file. 但是,FancyUpload仅允许您为每个文件指定元数据。 That is, I can have an input and add it as a POST parameter but then every file will receive that parameter. 也就是说,我可以有一个输入并将其添加为POST参数,但是每个文件都会收到该参数。 I need to be able to specify, say, a title for each file that is sent. 我需要能够为每个发送的文件指定标题。

Please note that this answer assumes a working knowledge of JavaScript and Mootools. 请注意,此答案假定您具有JavaScript和Mootools的使用知识。 I'll tackle each problem in turn: 我将依次解决每个问题:

Rendering the Inputs 渲染输入

It turns out that the rendering of each file is handled by a method on the FancyUpload2.File class. 事实证明,每个文件的呈现均由FancyUpload2.File类上的方法处理。 To alter it, we will need to implement it as follows: 要对其进行更改,我们将需要如下实现:

FancyUpload2.File.implement({
    render: function() {
    //copy the entire render function in here
    }
});

Make sure you copy the entire render function across. 确保跨整个复制渲染功能。 At the moment, this will just override the default render method with... the default render method. 目前,这将使用...默认渲染方法覆盖默认渲染方法。 However, we only want to alter the rendering process, not rewrite it so this is good. 但是,我们只想更改渲染过程,而不要重写它,所以很好。 About halfway through the method is the line this.element = new Element('li', {'class': 'file'}).adopt( and then a list of elements inside the adopt method that FancyUpload will render. At the very end of the adopt method, enter the following: 大约在方法的一半处是以下行: this.element = new Element('li', {'class': 'file'}).adopt(然后是FancyUpload将呈现的accept方法内的元素列表。在采用方法的最后,输入以下内容:

new Element('input', {'class': 'caption', 'name': 'caption[]', 'type': 'text'})

As you can see this will add a new input to the list, intended to get a caption from the user. 如您所见,这会将新的输入添加到列表中,旨在从用户那里获得标题。 You might also want to add in a label and any other form elements. 您可能还需要添加标签和任何其他表单元素。

Now if you select some files, you will notice that they are all rendered with the extra elements you have specified. 现在,如果您选择一些文件,您会注意到它们全部使用指定的额外元素渲染。

Posting the Inputs 张贴输入

When you create the FancyUpload object, you need to specify a new event in its options object: 创建FancyUpload对象时,需要在其options对象中指定一个新事件:

onBeforeStart: function() {
    var listSize = this.fileList.length;
    for (var i=0; i < listSize; i++){
        var caption = this.fileList[i].element.getElement('input.caption').get('value');
        var opts = $merge(this.options.data, {
            'caption': caption
        });
        this.fileList[i].setOptions({'data' : opts});
    }
}

onBeforeStart gets fired just before we send the AJAX request. 在发送AJAX请求之前,onBeforeStart被触发。 This means anything we do here will be done after the user has finished entering data and before that data gets sent. 这意味着我们在用户完成输入数据之后以及发送数据之前将执行此处的所有操作。 Brilliant! 辉煌! We need to add the new data to each file so the first thing to do is iterate over the file list. 我们需要将新数据添加到每个文件,因此要做的第一件事是遍历文件列表。 Then we find the value of caption input associate with the current file in the list and assign it to the var caption . 然后,在列表中找到与当前文件关联的caption输入的值,并将其分配给var caption

Then we access the files data with this.options.data and use $merge to create a new object with all that data and our new data as well. 然后,我们使用this.options.data访问文件数据,并使用$merge创建一个包含所有数据和新数据的新对象。 We assign this object to var opts . 我们将此对象分配给var opts Finally we overwrite the files original options by using setOptions . 最后,我们使用setOptions覆盖文件的原始选项。 Now when the file is sent, out data gets sent along with it and will be accessible from $_POST if you are using PHP. 现在,发送文件后,输出的数据将随文件一起发送,如果使用PHP,则可以从$ _POST访问。

Credits: I found the key to the solution to the second problem in this post . 鸣谢:我在这篇文章中找到了解决第二个问题的关键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP计算目录中的所有文件? (例如124个文件) - How can I count all files in a directory with PHP? (e.g. 124 Files) 如何在PHP中使用Ajax从网站(例如IMDB)中获取数据 - How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP 如何选择0作为前缀的值,例如00-09 - How can I select values with 0 as prefix e.g. 00-09 如何检查用户是否通过Google Adwords访问(例如,使用__utm * coockie)? - How can I check if a user came via Google Adwords (e.g. by using the __utm* coockie)? 如何在Codeception帮助器中访问演员(例如AcceptanceTester) - How can I access an actor (e.g. AcceptanceTester) in a Codeception helper 如何在特定时间间隔内运行PHP脚本(例如每天一次)? - How can I run PHP script in certain interval (e.g. once a day)? 我如何以编程方式查看网站发出的请求(例如 API 或资源请求) - How can I programmatically see what requests (e.g. API or resource requests) are being made by a website 如何对 SQL 查询进行排序,但将某些 UTF-8 字符排序为正常等效字符? (例如É被视为E等) - How can I sort an SQL query but have certain UTF-8 characters be ordered as their normal equivalent? (e.g. É be regarded as E etc) MySQL - 按星期几比较月份的数据 - 例如每个月第一个星期六的会话 - MySQL - Compare data from months by day of week - e.g. sessions on first Saturday of each month 多个文件上的切换语句(例如,用于切换情况语句的插件系统) - Switch-Statement over multiple files (e.g. Plugin-System for switch-case-statements)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM