您好,我收到了以下代码:

$(document).ready(function(){

$('#upload-file-selector').change(function(){

     var $currID = $("#upload-file-selector").attr('data-uid')

    $(this).simpleUpload("uploadFile.php", {

        start: function(file){
            //upload started
            $('#filenameD').html(file.name);
            $('#progressD').html("");
            $('#progressBarD').width(0);
        },

        progress: function(progress){
            //received progress
            $('#progressD').html("Progress: " + Math.round(progress) + "%");
            $('#progressBarD').width(progress + "%");
        },

        success: function(data){
            //upload successful
            $('#progressD').html("Success!<br>Data: " + JSON.stringify(data));
        },

        error: function(error){
            //upload failed
            $('#progressD').html("Failure!<br>" + error.name + ": " + error.message);
        }

    });

});

});


它可以与此插件一起使用:
SimpleUpload

如您所见,我有var currID。如何将这个ID和文件一起发送到PHP?如何将var绑定到JSON-String?

最佳答案

如果要通过POST传递$currID,请对SimpleUpload使用data选项。

http://simpleupload.michaelcbrook.com/#settings


  数据对象-一组键值对,包含要与每个文件一起发送到服务器的POST数据。周围的任何表单都不会自动填充它。


使用您的代码:

$(document).ready(function(){
  $('#upload-file-selector').change(function(){;
    var $currID = $("#upload-file-selector").attr('data-uid')
    $(this).simpleUpload("uploadFile.php", {
      data: {
        "id": $currID
      },
      start: function(file){
        //upload started
        $('#filenameD').html(file.name);
        $('#progressD').html("");
        $('#progressBarD').width(0);
      },
      progress: function(progress){
        //received progress
        $('#progressD').html("Progress: " + Math.round(progress) + "%");
        $('#progressBarD').width(progress + "%");
      },
      success: function(data){
        //upload successful
        $('#progressD').html("Success!<br>Data: " + JSON.stringify(data));
      },
      error: function(error){
        //upload failed
        $('#progressD').html("Failure!<br>" + error.name + ": " + error.message);
      }
    });
  });
});

09-28 05:37