Jquery文件上载蓝色Imp-设置动态上载url

Jquery文件上载蓝色Imp-设置动态上载url,jquery,file-upload,blueimp,Jquery,File Upload,Blueimp,我正在使用Jquery上传文件库,在设置要将文件写入的目录时遇到困难。如果使用示例中设置的标准位置,文件将上载。 以下代码起作用: $('#fileupload').fileupload({ disableImageResize: false, autoUpload: false, // Uncomment the following to send cross-domain cookies: //

我正在使用Jquery上传文件库,在设置要将文件写入的目录时遇到困难。如果使用示例中设置的标准位置,文件将上载。 以下代码起作用:

 $('#fileupload').fileupload({
            disableImageResize: false,
            autoUpload: false,
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true}, 
             //url: currentDir
            url: '/assets/plugins/jquery-file-upload/server/php/'
        });
但如果我尝试按以下方式更改url:

    var currentDir = window.location.pathname;

return {
    //main function to initiate the module
    init: function () {

         // Initialize the jQuery File Upload widget:
        $('#fileupload').fileupload({
            disableImageResize: false,
            autoUpload: false,
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true}, 
             url: currentDir
            //url: '/assets/plugins/jquery-file-upload/server/php/'
        });

我收到这个错误:SyntaxError:Unexpected token问题是
URL
变量是定位PHP脚本的位置。它不是上载文件的位置

我也有类似的问题。我通过向脚本传递一个额外的变量来解决这个问题,并在php中更改
upload\u dir
变量

您的默认index.php文件应该如下所示

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler();
我对它进行了修改,以侦听从JS传递的额外变量。看看评论,看看发生了什么。从本质上讲,它确保在调用此文件(从javascript)时收到名为
fuPath
的额外变量,如果没有,则不要上载该文件。如果收到该路径,则使用它根据web根目录获取最终目的地

    error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
require('UploadHandler.php');
$urlHolder = NULL;


// This option will be used while uploading the file.
// The path will come as POST request.
if( isset($_POST['fuPath']) ) {
    // perform validations to make sure the path is as you intend it to be
    $urlHolder = filter_var($_POST['fuPath'], FILTER_SANITIZE_URL);
}
// This option will be used when deleting a file.
// The file details will come from GET request so have to be careful
else if( isset($_GET['fuPath']) ){
    // perform validations to make sure the path is as you intend it to be
    $urlHolder = filter_var($_GET['fuPath'], FILTER_SANITIZE_URL);
}
else{
    exit;
}
$options = array(
                'upload_dir'=>  $_SERVER['DOCUMENT_ROOT'] .'/' .$urlHolder,
//              'upload_url'=>'server/php/dummyXXXX',   // This option will not have any effect because thumbnail creation is disabled
                'image_versions' => array(),            // This option will disable creating thumbnail images and will not create that extra folder.
                                                        // However, due to this, the images preview will not be displayed after upload
            );

if($urlHolder){
    $upload_handler = new UploadHandler($options , true , null);
}
现在在JS方面,我做了以下更改

var fullUpldPath = "";
// when file upload form is initialised then along with other options I also set the file upload path "fuPath".
// default will be empty string, so we can validate in php code.
$(localFormNames.fileForm).fileupload({
    formData: {fuPath: fullUpldPath}
});
现在回到HTML。我创建了一个虚拟按钮(让我们调用#uploadFiles),用户点击该按钮可以上传图像。这是必需的,因为我想在用户单击上载和上载开始之间操作这个
fuPath
变量。插件的上传按钮(我们称之为#pluginButton)是隐藏的

因此,当用户单击“上载文件”时,我会根据需要更改文件上载路径。设置修改后的变量,然后单击实际插件的上载按钮

// ending / is mandatory.
fullUpldPath = "assets/uploadedImages" + $("#imagename").val() + "/";
$(localFormNames.fileForm).fileupload({
    formData: {fuPath: fullUpldPath}
});
// Now manually trigger the file upload
$("#pluginButton").click();

试试这个:{var currentDir=null;$(function(){currentDir=window.location.pathname;//代码的其余部分});}谢谢你试过了,但没有什么乐趣。我想你应该给我们完整的js代码,如果它在HTML中,也会被请求的。:)