PHP+ExtJS 檔案上傳樣本

來源:互聯網
上載者:User

標籤:

xtJS 4 有一個非常方便的檔案上傳組件,可以用來將檔案上傳到伺服器。本文PHP教程UncleToo將介紹使用PHP和ExtJS實現檔案上傳功能。

       首先,建立檔案上傳組件Ext.form.Panel,並添加一個上傳按鈕及按鈕單擊事件,該事件將驗證並提交表單到upload.php的檔案。看下面代碼:

 

ExtJS部分

12345678910111213141516171819202122232425262728293031323334353637383940 Ext.onReady(function () {  Ext.widget(‘form‘, {    title: ‘Upload Demo‘,    width: 400,    bodyPadding: 10,    items: [{      xtype: ‘filefield‘,      name: ‘file‘,      fieldLabel: ‘File‘,      labelWidth: 50,      anchor: ‘100%‘,      buttonText: ‘Select File...‘    }],    buttons: [{      text: ‘Upload‘,      handler: function () {        var form = this.up(‘form‘).getForm();        if (form.isValid()) {          form.submit({            url: ‘/extjs-tutorials/upload.php‘,            waitMsg: ‘Uploading your file...‘,            success: function (f, a) {              var result = a.result, data = result.data,                name = data.name, size = data.size,              message = Ext.String.format(‘<b>Message:</b> {0}<br>‘ +                ‘<b>FileName:</b> {1}<br>‘ +                ‘<b>FileSize:</b> {2}‘,                result.msg, name, size);              Ext.Msg.alert(‘Success‘, message);            },            failure: function (f, a) {              Ext.Msg.alert(‘Failure‘, a.result.msg);            }          });        }      }    }],    renderTo: ‘output‘  });});

效果預覽:

 

Upload.php檔案

12345678910111213141516171819 <?phpif ($_FILES["file"]["error"] > 0){  $error  = $_FILES["file"]["error"];  $response = array(‘success‘ => false, ‘msg‘ => $error);  echo json_encode($response);}else{  $file_name = $_FILES["file"]["name"];  $file_type = $_FILES["file"]["type"];  $file_size = round($_FILES["file"]["size"] / 1024, 2) . "  Kilo Bytes";  $response = array(‘success‘ => true,    ‘data‘ => array(‘name‘ => $file_name, ‘size‘ => $file_size),    ‘msg‘ => ‘File Uploaded successfully‘  );  echo json_encode($response);}?>

選擇要上傳的檔案,並點擊上傳按鈕,效果如下:

PHP+ExtJS 檔案上傳樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.