url)) { if ($this->onlyImage === false) { $this->url = $this->multiple ? ['/upload/files-upload'] : ['/upload/file-upload']; // $this->acceptFileTypes = 'image/png, image/jpg, image/jpeg, image/gif, image/bmp, application/x-zip-compressed'; } else { $this->url = $this->multiple ? ['/upload/images-upload'] : ['/upload/image-upload']; // $this->acceptFileTypes = 'image/png, image/jpg, image/jpeg, image/gif, image/bmp'; } } if ($this->hasModel()) { $this->name = $this->name ? : Html::getInputName($this->model, $this->attribute); $this->attribute = Html::getAttributeName($this->attribute); $value = $this->model->{$this->attribute}; $attachments = $this->multiple == true ? $value :[$value]; $this->value = []; if ($attachments) { foreach ($attachments as $attachment) { $value = $this->formatAttachment($attachment); if ($value) { $this->value[] = $value; } } } } $this->fileInputName = md5($this->name); if (! array_key_exists('fileparam', $this->url)) { $this->url['fileparam'] = $this->fileInputName;//服务器需要通过这个判断是哪一个input name上传的 } $this->clientOptions = ArrayHelper::merge($this->clientOptions, [ 'id' => $this->options['id'], 'name'=> $this->name, //主要用于上传后返回的项目name 'url' => Url::to($this->url), 'multiple' => $this->multiple, 'sortable' => $this->sortable, 'maxNumberOfFiles' => $this->maxNumberOfFiles, 'maxFileSize' => $this->maxFileSize, 'acceptFileTypes' => $this->acceptFileTypes, 'files' => $this->value?:[] ]); } protected function formatAttachment($attachment) { if (!empty($attachment) && is_string($attachment)) { return [ 'url' => $attachment, 'path' => $attachment, ]; } else if (is_array($attachment)) { return $attachment; } else if ($attachment instanceof Arrayable) return $attachment->toArray(); return []; } /** * * @return string */ public function run() { $this->registerClientScript(); $content = Html::hiddenInput($this->name . ($this->multiple ? '[]' : ''), null, $this->options); $content .= Html::beginTag('div',$this->wrapperOptions); $content .= Html::fileInput($this->fileInputName, null, [ 'id' => $this->fileInputName, 'multiple' => $this->multiple, 'accept' => $this->acceptFileTypes ]); $content .= Html::endTag('div'); return $content; } /** * Registers required script for the plugin to work as jQuery File Uploader */ public function registerClientScript() { Html::addCssClass($this->wrapperOptions, "upload-kit"); AttachmentUploadAsset::register($this->getView()); if ($this->sortable) { JuiAsset::register($this->getView()); } $this->clientOptions['onlyUrl'] = $this->onlyUrl; $options = Json::encode($this->clientOptions); $this->getView()->registerJs("jQuery('#{$this->fileInputName}').attachmentUpload({$options});"); } }