我目前有一个表单,其中的上传文件如下所示:
我想使它看起来像这样:
之所以这样做,是因为服务器已经有文件。所以我只想在输入字段中添加URL。但是,上传按钮也很重要,因为我们将需要上传服务器中尚未存在的文件。
我们如何最好地解决这个问题?
我目前有以下代码:
function wp_custom_attachment() {
wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';
echo $html;
}
我尝试了这样的方法,但最终得到警告:
$html .= '<input type="text" value="" id="wp_custom_attachment" name="wp_custom_attachment" onclick="javascript:document.getElementById(\'file\').click();"><input id="file" type="file" name="img" onchange="ChangeText(this, \'wp_custom_attachment\');"/>';
警告:
Warning: Illegal string offset 'url' in /path/content.php on line 42
答案 0 :(得分:1)
您可以通过使用type='file'
的隐藏输入,button
和输入type='text'
来完成此操作。为了使按钮处于活动状态,我们在按钮的onclick
处添加了一个触发器。输入onchange
的{{1}}会触发函数type='file'
来更改输入changeInput
的内容。
这将导致以下type='text'
:
html
请注意,此<input type="text" id="filename" name="filename"><button onclick="document.getElementById('file').click();">Open</button>
<input id="file" type="file" name="file" style="display: none;" onchange="changeInput()" />
<script>
function changeInput(){
document.getElementById("filename").value = document.getElementById('file').value;
}
</script>
将文件的完整路径放入html
输入中。保存新文件时,需要对已编辑的文件名进行一些解析。