重命名上传文件的文件名,用item_id扩展

时间:2015-07-16 10:10:40

标签: javascript php ajax file

我希望数字123456是文件名的前缀(" Picture.jpg")。

原始文件名应重命名为" 123456Picture.jpg"

我在" mulitupload.js"中有这一行: data.append(' ITEM_ID'' 123456&#39);

不幸的是文件名总是" 000000Picture.jpg"

file" upload.php"

            <?php

            //If directory doesnot exists create it.
            $output_dir = "sobimages/";

            $new_id = sprintf( "%06d", $_POST['item_id']);

            if(isset($_FILES["myfile"]))
            {
                $ret = array();

                $error =$_FILES["myfile"]["error"];
                {

                    if(!is_array($_FILES["myfile"]['name'])) //single file
                    {
                        $fileName = $_FILES["myfile"]["name"];
                        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$new_id.$_FILES["myfile"]["name"]);
                        //echo "<br> Error: ".$_FILES["myfile"]["error"];

                        $ret[$fileName]= $output_dir.$fileName;
                    }
                    else
                    {
                        $fileCount = count($_FILES["myfile"]['name']);
                        for($i=0; $i < $fileCount; $i++)
                        {
                            $fileName = $_FILES["myfile"]["name"][$i];
                            $ret[$fileName]= $output_dir.$fileName;
                            move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName );
                        }

                    }
                }
                echo json_encode($ret);

            }

            ?>

file&#34; multiupload.js&#34;

            function multiUploader(config){

                this.config = config;
                this.items = "";
                this.all = []


                var self = this;

                multiUploader.prototype._init = function(){
                    if (window.File &&
                        window.FileReader &&
                        window.FileList &&
                        window.Blob) {
                         var inputId = $("#"+this.config.form).find("input[type='file']").eq(0).attr("id");
                         document.getElementById(inputId).addEventListener("change", this._read, false);
                         document.getElementById(this.config.dragArea).addEventListener("dragover", function(e){ e.stopPropagation(); e.preventDefault(); }, false);
                         document.getElementById(this.config.dragArea).addEventListener("drop", this._dropFiles, false);
                         document.getElementById(this.config.form).addEventListener("submit", this._submit, false);
                    } else
                        console.log("Browser supports failed");
                }

                multiUploader.prototype._submit = function(e){
                    e.stopPropagation(); e.preventDefault();
                    self._startUpload();
                }

                multiUploader.prototype._preview = function(data){
                    this.items = data;
                    if(this.items.length > 0){
                        var html = "";
                        var uId = "";
                        for(var i = 0; i<this.items.length; i++){
                            uId = this.items[i].name._unique();
                            var sampleIcon = '<img src="images/image.png" />';
                            var errorClass = "";
                            if(typeof this.items[i] != undefined){
                                if(self._validate(this.items[i].type) <= 0) {
                                    sampleIcon = '<img src="images/unknown.png" />';
                                    errorClass =" invalid";
                                }
                                html += '<div class="dfiles'+errorClass+'" rel="'+uId+'"><h5>'+sampleIcon+this.items[i].name+'</h5><div id="'+uId+'" class="progress" style="display:none;"><img src="images/ajax-loader.gif" /></div></div>';
                            }
                        }
                        $("#dragAndDropFiles").append(html);
                    }
                }

                multiUploader.prototype._read = function(evt){
                    if(evt.target.files){
                        self._preview(evt.target.files);
                        self.all.push(evt.target.files);
                    } else
                        console.log("Failed file reading");
                }

                multiUploader.prototype._validate = function(format){
                    var arr = this.config.support.split(",");
                    return arr.indexOf(format);
                }

                multiUploader.prototype._dropFiles = function(e){
                    e.stopPropagation(); e.preventDefault();
                    self._preview(e.dataTransfer.files);
                    self.all.push(e.dataTransfer.files);
                }

                multiUploader.prototype._uploader = function(file,f){
                    if(typeof file[f] != undefined && self._validate(file[f].type) > 0){
                        var data = new FormData();
                        var ids = file[f].name._unique();
                        data.append('file',file[f]);
                        data.append('index',ids);

                        // I want that the number 123456 is the prefix of the filename
                        data.append('item_id','123456');

                        $(".dfiles[rel='"+ids+"']").find(".progress").show();
                        $.ajax({
                            type:"POST",
                            url:this.config.uploadUrl,
                            data:data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success:function(rponse){
                                $("#"+ids).hide();
                                var obj = $(".dfiles").get();
                                $.each(obj,function(k,fle){
                                    if($(fle).attr("rel") == rponse){
                                        $(fle).slideUp("normal", function(){ $(this).remove(); });
                                    }
                                });
                                if (f+1 < file.length) {
                                    self._uploader(file,f+1);
                                }
                            }
                        });
                    } else
                        console.log("Invalid file format - "+file[f].name);
                }

                multiUploader.prototype._startUpload = function(){
                    if(this.all.length > 0){
                        for(var k=0; k<this.all.length; k++){
                            var file = this.all[k];
                            this._uploader(file,0);
                        }
                    }
                }

                String.prototype._unique = function(){
                    return this.replace(/[a-zA-Z]/g, function(c){
                       return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
                    });
                }

                this._init();
            }

            function initMultiUploader(){
                new multiUploader(config);
            }

0 个答案:

没有答案