动态更改Jquery文件上传URL

时间:2014-06-29 21:52:46

标签: javascript php jquery file-upload

我在我的项目中使用jQuery File Upload。我在动态更改URL时遇到问题,即我想动态更改上传文件夹。我看过一些帖子,例如

jQuery File Upload: how to change the upload url dynamically

https://github.com/blueimp/jQuery-File-Upload/issues/2640

但是这些都告诉要更改“data.url”,但data.url会更改上传PHP文件的文件夹。

我可以通过在

上传handler.php中更改这些行来使用会话来解决这个问题
'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$_SESSION['cid'].'/',
'upload_url' => $this->get_full_url().'/'.$_SESSION['cid'].'/',

但是如果用户同时打开两个不同的页面会发生什么,那么它会将所有页面上传到同一个文件夹,因为会话变量不会更新。有没有任何可能的解决方案使用Javascript?所以我不用担心。任何帮助将不胜感激。

由于

3 个答案:

答案 0 :(得分:3)

唯一的解决方案是通过在

上传handler.php中更改这些行来使用会话来解决它
'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$_SESSION['cid'].'/',
'upload_url' => $this->get_full_url().'/'.$_SESSION['cid'].'/',

答案 1 :(得分:0)

这可能不是您可能正在寻找的确切答案,但按照这些步骤可以找到答案。

我有类似的问题。我通过将额外的变量传递给脚本并在php中更改upload_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)是隐藏的。

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

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

答案 2 :(得分:0)

我使用用户目录。看到: https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-user-directories 的index.php

require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {
    protected function get_user_id() {
        @session_start();
        return $_SESSION['mySubDynamicDir'];
    }
}
$upload_handler = new CustomUploadHandler(array(
    'user_dirs' => true
));