我正在努力处理上传文件所需的一些道场。现在文件上传表单位于一个dojo对话框中,因此在用户选择“上传文件”按钮之前一直隐藏。
此按钮可以在网站上的任何位置点击,因此我创建了一个控制器来处理上传。
目前我只是想让它发挥作用,在我的头像中我有以下内容:
<?php $this->headScript()->captureStart(); ?>
function sendForm(){
//Hide the file input field
dojo.style('inputField',"display","none");
//Show the progress bar
dojo.style('progressField',"display","inline");
dojo.byId('preamble').innerHTML = "Uploading ...";
dojo.io.iframe.send({
url: "<?php echo $this->baseUrl(); ?>/fileprocssing/loadfile/",
method: "post",
handleAs: "text",
form: dojo.byId('StartFrm'),
handle: function(data,ioArgs){
var fileData = dojo.fromJson(data);
if (fileData.status == "success"){
//Show the file input field
dojo.style(dojo.byId('inputField'),"display","inline");
dojo.byId('fileInput').value = '';
//Hide the progress bar
dojo.style(dojo.byId('progressField'),"display","none");
dojo.byId('uploadedFiles').innerHTML += "success: File: " + fileData.details.name
+ " size: " + fileData.details.size +"<br>";
dojo.byId('preamble').innerHTML = "File to Upload: ";
}else{
dojo.style(dojo.byId('inputField'),"display","inline");
dojo.style(dojo.byId('progressField'),"display","none");
dojo.byId('preamble').innerHTML = "Error, try again: ";
}
}
});
}
<?php $this->headScript()->captureEnd() ?>
使用这样的基本上传
<form id="StartFrm" enctype="multipart/form-data"
name="cvupload"
action="<?php echo $this->baseUrl();?>/fileprocssing/loadfile/"
method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<!-- wrapping these in spans to be able to modify
parts of this form depending on what the
dojo.io.iframe.submit() does -->
<span id="preamble">File to Upload:</span><br>
<span id="inputField">
<input type="file" id="fileInput" name="uploadFile">
</span>
<span id="progressField" style="display:none;">
<div dojoType="dijit.ProgressBar" style="width:200px" indeterminate="true"></div>
</span>
<br/>
<button value="upload" dojoType="dijit.form.Button"
onclick="sendForm()">Upload</button>
</form>
我想知道的是,如果从/ somecontroller / someaction / ??调用表单,我如何从/ fileprocssing / loadfile /获取包含上传数据信息的JSON数据对象?当文件处理完毕后,会自动重定向到/ fileprocesing / reviewdata /
目前我的行动看起来像这样
public function loadfileAction() {
$log = Zend_Registry::getInstance()->get('log');
$log->log('in loadfileaction', Zend_Log::DEBUG);
$log->log($_FILES['uploadFile']['name'], Zend_Log::DEBUG);
$uploadedFile = array(
'details' => $_FILES['uploadFile'],
'status' => 'success'
);
$log->log($fileUploadData->toJson(), Zend_Log::DEBUG);
$foo = "{'status':'success',details: {name:'".
$_FILES['uploadFile']['name'].
"',size:".
$_FILES['uploadFile']['size'].
"}}";
$log->log($foo, Zend_Log::DEBUG);
$this->view->fileData = $foo;
}
我暂时手工制作了JSON数据,但是会使用Zend_Dojo_Data,但目前我只是想让它正常工作。
我必须承认我不太了解道场,但试图在最短的时间内解决它。
提前致谢。
答案 0 :(得分:2)
dojo.io.iframe.send要求将响应数据包装在TEXTAREA标记中。这是成功访问和加载响应数据的唯一/最简单的跨浏览器方式,这是一项要求。看起来你正在从动作中发回普通的JSON。
您还可以将句柄调整为“json”并跳过中间的dojo.fromJson(数据)调用,它将作为JSON对象传递给您(前提是响应包含在前面提到的TEXTAREA中)