如何使用VSd发送ajax请求,ajax - Eajaxupload shows "failed" everytime - Stack Overflow

It work for me:

$folder = Yii::app()->basePath . '/uploads/documents/';

Where my root folder is:

c:\wamp\www\protected\uploads\documents\

View file:

<?php $this->widget('ext.EAjaxUpload.EAjaxUpload',

array(

'id'=>'uploadFile',

'config'=>array(

'action'=>Yii::app()->createUrl('documents/addDocumentActivitatByAjax').'/'.$modelActivitat->id,

'allowedExtensions'=>array('jpeg','jpg','gif','png','txt','doc','docx','xls','xlsx','xml','ppt','pptx','pdf', 'vsd'),

'sizeLimit'=>10*1024*1024,// Tamaño máximo del fichero en bytes (10M)

'minSizeLimit'=>1024,// Tamaño minimo del fichero en bytes

'onComplete'=>"js:function(id, fileName, responseJSON){

if(responseJSON.success){

var msg = JSON.stringify(responseJSON);

addFlashMessage(msg);

$('#documents-grid').yiiGridView.update('documents-grid');

}

}",

),

)); ?>

Controller:

/**

* Recibe por ajax un campo tipo 'file' y lo guarda como 'documents'

* @param id Recibido por GET como parametro es el valor asignado al campo 'id_relacio' del Document a crear

*/

public function actionAddDocumentActivitatByAjax(){

$id_relacio = isset($_GET['id'])? $_GET['id'] : null;

if(!empty($id_relacio)){

$this->addDocument(LEME_DOCUMENTS_TIPUS_RELACIO_ACTIVITAT, $id_relacio);

}else{

echo htmlspecialchars(json_encode(array('success'=>'false', 'error'=>'No s\'ha rebut el id de l\'activitat associada')), ENT_NOQUOTES);

}

}

/**

* Recibe por ajax un campo tipo 'file' y lo guarda como 'documents'

* @param tipus_relacio Id del tipo de relación que guardará el Document

* @param id Recibido como parametro es el valor asignado al campo 'id_relacio' del Document a crear

*/

public function addDocument($tipus_relacio, $id_relacio){

Yii::import("ext.EAjaxUpload.qqFileUploader");

$folder = Yii::app()->basePath.$this->carpetaContingut;

// $allowedExtensions = array('jpeg','jpg','gif','png','txt','doc','docx','xls','xlsx','xml','ppt','pptx','pdf', 'vsd');

// $sizeLimit = 25 * 1024 * 1024;// 25M

$uploader = new qqFileUploader();

$originalName = $uploader->file->getName();

$originalName = SettingHelper::eliminaAcentos($originalName);

// Guardamos el archivo con nombre 'MD5()'

$result = $uploader->handleUpload($folder);

if(!isset($result['error'])){

// Si se sube bien generamos el registro en documents y renombramos el archivo recien subido a 'idDocument-NombreArchivoSubido.ext'

// $result['fileSize'] = filesize($folder.$result['filename']);//GETTING FILE SIZE

$fileNameMD5=$result['filename']; // Nombre del fichero guardado en encriptado en MD5

$document = new Documents();

$document->tipus_relacio = $tipus_relacio; // ID referent al tipus de relacio segons la taula vinculada

$document->id_relacio = $id_relacio; // ID del registre al que pertany

$document->path = $this->carpetaContingut;

if($document->save()){

// Renombramos el archivo recien subido con el nuevo nombre : 'idDocument-NombreArchivoSubido.ext'

$newName = $document->id.'-'.$originalName;

if(rename($folder.$fileNameMD5, $folder.$newName)){

// Si se renombra, hacemos update del 'document' con el nombre del archivo final

$document->url = $newName;

$document->save();

$return = array('success'=>'true', 'estat'=>'success', 'text'=> 'El fitxer "'.$newName.'" s\'ha guardat correctament.');

}

}else{ // si da error al generar el document eliminamos el fichero recién subido.

unlink($folder.$fileNameMD5);

$return = array('success'=>'false', 'error'=> 'Error al generar el registre "document" associat.');

}

}else{ //Si da error al subir el fichero

$return = $result;

}

echo CJSON::encode($return);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值