如何附加响应数据?

时间:2016-04-18 09:07:49

标签: jquery laravel

在当前代码中,我在上传文件时收到文件名。上传后我爆炸了文件名,我得到的回复如下。我的问题是如何在jQuery中获取数据并使用html附加?

在jQuery警报响应中显示" Localization,dummy "如何单独附加这两个数据。

控制台:响应

{"success":true,"exploded_filename":["Localization","dummy"],"id":"2"}

控制台:Json

success              true
exploded_filename    ["Localization", "dummy"]
0                    "Localization"
1                    "dummy"
id                   "2"

Dropzone中的Jquery

Dropzone.options.myDropzone = {
            // Prevents Dropzone from uploading dropped files immediately
            autoProcessQueue: true,
            parallelUploads: 20,
            uploadMultiple: true,
            maxFilesize: 10,
            maxFiles: 20,
            init: function() {
                myDropzone = this; // closure
                // You might want to show the submit button only when
                // files are dropped here:
                this.on("success", function(file, responseText) {
                    myDropzone.removeFile(file);
                    if(responseText.success == true){
                        $(".createProcessEntriesFileUpload").show();
                        $("#my-dropzone").hide();
                        $("#filenameFormGroup").show();
                        $(".createProcessEntriesMessage").hide();
                        alert(responseText.exploded_filename);
                        $("#filename").append('<a class="btn btn-sm btn-default download" href="" data-file="">'+responseText.exploded_filename+'</a> <a href="" class="removeProcessEntryFile" style="color: #333; cursor: pointer;"><button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Add file detail</button></a><br>');

                        /*setTimeout(function() {
                         location.href = '<?php echo url("/");?>/customers/list/process/<?php echo $id;?>/entries';
                         }, 3000);*/
                    };
                });
            }
        };

控制器页面

public function storeFile(ProcessEntriesRequest $request, $id)
    {
        $files                     = $request->file('file');
        foreach($files as $file){
            $orginalName           = $file->getClientOriginalName();
            $split_file            = explode('.', $orginalName);
            $exploded_filename[]   = $split_file[0];
            $extension             = $file->getClientOriginalExtension();
            $filename              = str_random(10);
            Storage::disk('local')->makeDirectory($request->entryID, 0777);
            Storage::disk('local')->put($request->entryID.'/'.$filename.'.'.$extension,  File::get($file));
        }

        return response()->json(['success' => true, 'exploded_filename' => $exploded_filename, 'id' => $id]);
    }

2 个答案:

答案 0 :(得分:0)

使用join

console.log(responseText.exploded_filename.join(','));
 $("#filename").append('<a class="btn btn-sm btn-default download" href="" data-file="">'+responseText.exploded_filename.join('.')+'</a> <a href="" class="removeProcessEntryFile" style="color: #333; cursor: pointer;"><button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Add file detail</button></a><br>');

或 使用循环

var totalfiles = '<ul>';
$.each(responseText.exploded_filename,function(i,v){
   totalfiles+='<li><a class="btn btn-sm btn-default download" href="" data-file="">'+v+'</a></li>';
});
totalfiles+='</ul>'

 $("#filename").append(totalfiles+'<a href="" class="removeProcessEntryFile" style="color: #333; cursor: pointer;"><button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Add file detail</button></a><br>');

答案 1 :(得分:0)

Madalin是对的,您应该使用join方法。如果您拥有数组data = ['Localization', 'pdf'],那么data.join('.')将返回'Localization.pdf'See the doc here