保存后,Joomla设置`file`字段值

时间:2017-09-26 10:13:18

标签: php mysql file joomla upload

对于我的后端组件,我需要上传我的项目的图像。一切正常,我为图像添加字段:

$('body').on("keyup", '.emojionearea-editor', function (event) {  });

然后更新string url = "http://api-v2.happay.in/auth/v1/adduser"; string token = "fcd853a15a6e97b8834255dde74cd99527"; string dataToUpload = "{"requestedId":"12389","userId":"134474","firstName":"Maredu Laxmi","lastName":"","emailId":"arar@gmail.com","mobileNo":"096518644","dob":"1978-02-26","gender":"Female","title":"Ms","password":"","metaFields":{"EmpId":"12389","Grade":"D6","Location":"Hyderabad"},"supervisors":[{"supervisorId":"1024","roleName":"RP"},{"supervisorId":"1025","roleName":"RP"}]}"; var cli = new WebClient(); cli.Headers[HttpRequestHeader.ContentType] = "application/json"; cli.Headers[HttpRequestHeader.Authorization] = "Bearer " + token; string response = cli.UploadString(url, "POST", dataToUpload); 方法以控制上传:

<field
    type="file"
    name="category_image"
    accept="image/*"
    label="COM_SKYCATALOG_ITEM_CATEGORY_IMAGE_LABEL"
    directory=""
    preview="false"
/>

当我选择文件并保存项目时,文件上传到文件夹,但是类别图像字段为空。并且mysql表中的category_image字段也没有更新,但是当我使用save()字段类型时,它工作正常。

这里可能出现什么问题?

我不喜欢媒体经理让它放慢速度。

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。由于安全原因,您无法将input标记的值设置为file类型。我的意思是你可以设置value属性,但字段显示为空白。

所以我只是创建一个这样的自定义字段:

class JFormFieldSkyupload extends JFormField
{
    /**
     * The field type.
     *
     * @var         string
     */
    protected $type = 'Skyupload';

    /**
     * Method to get a list of options for a list input.
     *
     * @return  array  An array of JHtml options.
     */
    public function getInput() {
        return '<div class="filename_labe">'.$this->value.'</div><input name="'.$this->name.'" id="'.$this->id.'" accept="image/*" aria-invalid="false" type="file" value="'.$this->value.'">';
        // code that returns HTML that will be shown as the form field
    }

}

此外我还对上传代码进行了一些更改,因此即使没有选择新文件,我也可以保存表单而不会出错:

if ($files['category_image']['name']!="")
{
    $fileinfo =pathinfo($files['category_image']['name']);

    $ext = JFile::getExt($files['category_image']['name']);
    $filename = time().".$ext";

    $tmp_file = $files['category_image']['tmp_name'];

    $dest = "../media/com_skycatalog/upload/".$filename;

    if (JFile::upload($tmp_file, $dest)) {
    // successed :)
    } else {                  
    // failed :(
    echo "FAAAIL!!!";
    die();      
    }        

$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$data['category_image'] = strtolower( $filename );
JRequest::setVar('jform', $data );

}

现在只有在我上传新文件时它才会在DB中更新文件名。