plupload通过上传网址传递值

时间:2014-05-25 16:07:16

标签: javascript php html plupload

我使用plupload作为图片上传脚本。我希望能够将图像保存在一个或另一个文件夹中,具体取决于a中的选定选项。与......类似的东西。

<select id="dir">
    <option value="dir1">Option1</option>
    <option value="dir2">Option2</option>
</select>

初始化pluploader的代码包含上传脚本的URL:

url : "upload.php",

我希望能够将选项值传递给upload.php,所以我可以使用$ _GET来处理它们,例如:

url : "upload.php?d=" + dir,

我尝试过使用:

var element = document.getElementById("dir").selectedIndex;
var dir = document.getElementsByTagName("option")[element].value;

javascript代码:

<script type="text/javascript" language="javascript">
// Initialize the widget when the DOM is ready
$(function() {

    // Setup html5 version
    $("#uploader").pluploadQueue({
        // General settings
        runtimes : 'html5,flash,silverlight,html4',
        url : "../upload.php",

        chunk_size : '1mb',
        rename : true,
        dragdrop: true,

        filters : {
            // Maximum file size
            max_file_size : '10mb',
            // Specify what files to browse for
            mime_types: [
                {title : "Image files", extensions : "jpg,jpeg,gif,png"}
            ]
        },

        // Resize images on clientside if we can
        resize: {
            width : 800,
            height : 800,
            quality : 90,
            crop: false // crop to exact dimensions
        },


        // Flash settings
        flash_swf_url : '../js/Moxie.swf',

        // Silverlight settings
        silverlight_xap_url : '../js/Moxie.xap'
    });
});
</script>

但它始终获得第一个选项值,因为它在DOM就绪初始化...

我该怎么做?


解决:

1)添加:

$(function() {
    $("#uploader").pluploadQueue({ //...
        multipart: true,
        multipart_params: { 'dir': '' }, //...

2)添加:

var uploader = $('#uploader').pluploadQueue();
    uploader.bind('FilesAdded', function(up, files) {

    var element = document.getElementById("dir").selectedIndex;
    var dir = document.getElementsByTagName("option")[element].value;

    uploader.settings.multipart_params.dir = dir;       
});

来源:http://williamjxj.wordpress.com/2011/03/25/jquery-plugin-plupload-for-multi-files-upload/

1 个答案:

答案 0 :(得分:1)

我没有办法对此进行测试,但看起来您可以catch a BeforeUpload event并在那里进行更改:

var uploader = $("#uploader").pluploadQueue({
  // ...
});

uploader.bind("BeforeUpload", function() {
  var element = document.getElementById("dir").selectedIndex;
  var dir = document.getElementsByTagName("option")[element].value;
  uploader.settings.url = "upload.php?d=" + dir;
});