【问题标题】:Change Jquery file upload url dynamically动态更改 Jquery 文件上传 url
【发布时间】:2014-08-20 06:19:04
【问题描述】:

我在我的项目中使用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有没有可能的解决方案?所以我不需要担心。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: javascript php jquery file-upload


    【解决方案1】:

    我使用用户目录。看: 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
    ));
    

    【讨论】:

      【解决方案2】:

      唯一的解决方案是使用会话通过更改上传 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'].'/',
      

      【讨论】:

        【解决方案3】:

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

        我遇到过类似的问题。我通过向脚本传递一个额外的变量并在 php 中更改 upload_dir 变量来解决它。

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

        error_reporting(E_ALL | E_STRICT);
        require('UploadHandler.php');
        $upload_handler = new UploadHandler();
        

        我修改它来监听我从 JS 传递的额外变量。查看 cmets 以了解发生了什么。基本上发生的事情是确保在调用此文件(来自 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();
        

        【讨论】:

        • 谢谢,但这不起作用,如果我传递一个变量并更改 upload_dir 和 script_url 那么它适用于添加但对于删除,它找不到那个帖子变量,我无法发布它删除时
        • 这是一个好点。您要求上传文件,所以我只添加了那部分。您能否更新您的问题/就您如何实现 delete 功能提出一个新问题,以便我也可以将其包含在答案中。顺便说一句,这个答案确实解决了你原来的问题,那为什么不打勾呢?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 2022-11-29
        • 2012-09-06
        • 2014-01-03
        相关资源
        最近更新 更多