Javascript 使用模块在joomla中上载文件
我正在尝试创建一个特定的模块来上传文件 我正在使用以下代码: 客户端:Javascript 使用模块在joomla中上载文件,javascript,php,ajax,file-upload,joomla3.0,Javascript,Php,Ajax,File Upload,Joomla3.0,我正在尝试创建一个特定的模块来上传文件 我正在使用以下代码: 客户端: <?php // No direct access defined('_JEXEC') or die; $resposta =""; ?> <form name="upload" method="post" enctype="multipart/form-data"> <input type="file" name="file_upload" /> <input type="sub
<?php
// No direct access
defined('_JEXEC') or die; $resposta =""; ?>
<form name="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" />
<input type="submit" name="submit_file" value="submit_file"/>
<input type="text" name="resposta" value=<?php echo $resposta; ?> />
</form>
我的助手:
<?php
class ModuploadfileHelper {
public static function getuploadfile($params) {
/*
* File upload example
*/
//Retrieve file details from uploaded file, sent from upload form
$file = JFactory::getApplication()->input->get('file_upload');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;
if(!JFolder::exists($dest))
{
$mode = 0755;
JFolder::create($dest, $mode);
}
$resposta = null;
//First check if the file has the right extension, we need jpg only
if (strtolower(JFile::getExt($filename)) == 'jpg')
{
// TODO: Add security checks
if (JFile::upload($src, $dest))
{
$resposta = "Sucesso ao arquivar a imagem";
}
else
{
$resposta = "Insucesso ao arquivar a imagem";
}
}
else
{
$resposta = "O ficheiro não é uma imagem";
}
return $resposta;
}
}
?>
第一个问题:像这样的东西有用吗?
第二个问题:如何执行模块工作的触发器
第十三个问题:如何将模块传递给ajax?
我有这样的想法:
模块代码:
<?php
defined('_JEXEC') or die;
include_once __DIR__ . '/helper.php';
// Instantiate global document object
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';
$resposta = ModuploadfileHelper::getuploadfile($params);
defined('_JEXEC') or die;
include_once __DIR__ . '/helper.php';
// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
$(document).on('click', 'input[type=submit]', function () {
formdata = new FormData();
var file = this.files[0];
formdata.append("image", file);
$.ajax({
type : 'POST',
data : request,
success: function (response) {
$('.search-results').html(response);
}
});
return false;
});
})(jQuery)
JS;
$doc->addScriptDeclaration($js);
require JModuleHelper::getLayoutPath('mod_upload_file');
?>
请帮帮我。您需要做的是使用Joomla ajax接口 请参阅此处的文档: 有一个实现此功能的模块的完整示例,您可以轻松修改以适应文件上载:
我做了一些研究,据我所知,一些浏览器不支持通过ajax提交表单。我想保持文件上传简单。我不知道是哪个浏览器。。。但是如果你不想要ajax,我并不认为有什么问题,只是你的模块需要发布在表单发布的页面上,这样它就会被触发,并且可以作用于$\u FILES变量。或者更确切地说是JFactory::getApplication()->input->files->get('file_upload');更好的是,您甚至可以对“com_ajax”模块url执行常规post请求,并在处理上传后执行重定向。
<?php
defined('_JEXEC') or die;
include_once __DIR__ . '/helper.php';
// Instantiate global document object
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';
$resposta = ModuploadfileHelper::getuploadfile($params);
defined('_JEXEC') or die;
include_once __DIR__ . '/helper.php';
// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
$(document).on('click', 'input[type=submit]', function () {
formdata = new FormData();
var file = this.files[0];
formdata.append("image", file);
$.ajax({
type : 'POST',
data : request,
success: function (response) {
$('.search-results').html(response);
}
});
return false;
});
})(jQuery)
JS;
$doc->addScriptDeclaration($js);
require JModuleHelper::getLayoutPath('mod_upload_file');
?>