如何使用 PLupload 发送附加数据?

2022-01-09 00:00:00 upload jquery php plupload

我正在使用 plupload 进行 ajax 文件上传.现在 plupload.Uploader 类有很多选项,但没有一个是附加数据.

I'm using plupload to make an ajax file uploading. Now the plupload.Uploader class has many options but none are additional data.

例如:

var uploader = new plupload.Uploader({
    runtimes : 'gears,html5,flash,silverlight,browserplus',
    browse_button : 'pickfiles',
    container : 'contact_container',
    max_file_size : '10mb',
    url : 'upload.php',
    flash_swf_url : '/plupload/js/plupload.flash.swf',
    silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',
    filters : [
        {title : "Image files", extensions : "jpg,gif,png"},
        {title : "Zip files", extensions : "zip"}
    ],
    resize : {width : 320, height : 240, quality : 90}
});

我想要实现的是我的服务器中有一个文件夹,所有上传的内容都在其中正在被拯救.我需要在文件夹内为每个已上传文件的用户创建一个子文件夹.如何将用户 ID 等数据添加到 plupload.Uploader 的实例中?或者,如果我在容器 div 中包装一个表单,我能在 $_REQUEST 中看到它吗?或者有其他方法可以实现吗?

What i'm trying to achive is i have a folder in my server where all the uploads are being saved. I neeed inside the folder to create a sub-folder to each user that have uploaded files there. How can i add data like id of the user to the instance of plupload.Uploader? Or if i'll wrap a form inside the container div, will i be able to see it in the $_REQUEST? Or is there some other way i can achive this?

推荐答案

您是否尝试过使用 multipart_params 的设置?像这样向您的 plupload.Uploader 添加一个附加选项:

Have you tried using the setting for multipart_params? Add an additional option to your plupload.Uploader like so:

var uploader = new plupload.Uploader({
    runtimes : 'gears,html5,flash,silverlight,browserplus',
    browse_button : 'pickfiles',
    container : 'contact_container',
    max_file_size : '10mb',
    url : 'upload.php',
    flash_swf_url : '/plupload/js/plupload.flash.swf',
    silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',
    filters : [
        {title : "Image files", extensions : "jpg,gif,png"},
        {title : "Zip files", extensions : "zip"}
    ],
    resize : {width : 320, height : 240, quality : 90},
    multipart_params : {
        "name1" : "value1",
        "name2" : "value2"
    }
});

然后您需要处理处理上传的文件中的值(默认为upload.php).我认为这些值是由 $_POST 捕获的,但您可以使用 $_REQUEST 来确定.

You will then need to process the values in the file that handles the upload (upload.php by default). I think the values are captured by $_POST but you can use $_REQUEST just to be sure.

我使用 jQuery 动态分配值,因此您可以使用 "name1" : $("#name1" 之类的东西来代替 "name1" : "value1").val(), 其中#name1 可能是页面其他地方的输入.

I've used jQuery to assign values on the fly, so instead of "name1" : "value1" you can use something like "name1" : $("#name1").val(), where #name1 might be an input elsewhere on the page.

对于其中一些设置,Plupload 的文档有点稀疏.

Plupload's documentation is a little sparse for some of these settings.

相关文章