【问题标题】:File upload using EXTJS and PHP使用 EXTJS 和 PHP 上传文件
【发布时间】:2026-02-23 11:20:11
【问题描述】:

我使用 EXTJS 上传文件并保存在服务器中 问题是我无法获得成功功能,甚至 waitMsg 一直在运行 这是代码 EXTJS 代码:

 var fp = new Ext.FormPanel({
    fileUpload: true,
    width: 500,
    frame: true,
    title: 'File Upload Form',
    bodyStyle: 'padding: 10px 10px 0 10px;',
    labelWidth: 50,
    defaults: {
        anchor: '95%',
        allowBlank: false,
        msgTarget: 'side'
    },
    items: [{
        xtype: 'fileuploadfield',
        id: 'form-file',
        emptyText: 'Selectionnez un fichier',
        fieldLabel: 'Fichier',
        name: 'photo-path',
        buttonText: 'Parcourir'
    }],
    buttons: [{
        text: 'Ajouter',
        handler: function(){
            if(fp.getForm().isValid()){
     var myUrl = 'php/file-upload.php?idOrganisme=' + idOrganisme + '&demarche=' + demarche;
     //alert(Ext.getCmp('').);
                 fp.getForm().submit({
                     url: myUrl,
                     waitMsg: 'Envoi de votre fichier...',
      success:function(fp,o){
       alert('Sucess');

      }
                 });
            }
        }
    },{
        text: 'Fermer',
        handler: function(){

        }
    }]
});

PHP 代码:

<?php
 include("connexion.php");

 $server = "http://localhost/wa";
 $idOrganisme = $_GET['idOrganisme'];
 $demarche = $_GET['demarche'];

 $req = $bdd->query('SELECT idDemarche FROM demarche,typeDemarche WHERE
 idOrganisme=' . $idOrganisme . 
 ' AND demarche.idTypeDemarche = typeDemarche.idTypeDemarche 
 AND typeDemarche.typeDemarche="' . $demarche .'"');
 $demarcheArray = $req->fetch();
 $idDemarche = $demarcheArray['idDemarche'];

 $myPath = '../uploads/_' . $idOrganisme . '_' . $idDemarche . '/';

 $target_path = utf8_decode($myPath);
 mkdir($target_path, 0705);
 echo 'OK';
 if(isset($_FILES)){
  echo 'OK';
  $temp_file_name = utf8_decode($_FILES['photo-path']['tmp_name']);
  $original_file_name = utf8_decode($_FILES['photo-path']['name']);

  // Find file extention
  $ext = explode ('.', $original_file_name);
  $ext = $ext [count ($ext) - 1];

  // Remove the extention from the original file name
  $file_name = str_replace ($ext, '', $original_file_name);

  $new_name = $target_path . '[' . utf8_encode($file_name) . $ext;
  chmod($new_name, 705);

  if(move_uploaded_file ($temp_file_name, $new_name)) {
   if($idDemarche!=''){
    $length = strlen($new_name);
    $path = $server . substr($new_name,2,$
    $req = $bdd->prepare('INSERT INTO liens(idDemarche, lien) values(:idDemarche, :lien)');
    $req->execute(array('idDemarche' => intVal($idDemarche), 'lien' => $path));
    echo "OK";
   }
  } 
  else {
   echo "error";
  }
  echo 'message';
 }

?>

【问题讨论】:

    标签: php file-upload extjs


    【解决方案1】:

    遇到了类似的问题。发现您需要在处理文件上传的页面上将内容类型设置为“text/html”。 :-(

    Response.ContentType = "text/html";
    

    如果你read the documentation for Ext.data.Connection,你会看到

    浏览器解析服务器响应,为 IFRAME 创建文档。如果服务器使用 JSON 发送返回对象,则 Content-Type 标头必须设置为“text/html”,以便告诉浏览器将文本原封不动地插入到文档正文中。

    我花了一段时间才找到这个,但是当我遇到你的问题时,其他人可能会遇到类似的问题!

    希望这会对他们有所帮助!

    【讨论】:

      【解决方案2】:

      您需要为文件上传返回一个 JSON 响应,在您的 php 代码中使用:

      echo "{success: true}";
      

      echo "{success: false}";
      

      更多参考请参考:http://dev.sencha.com/deploy/dev/docs/source/Action.html#cls-Ext.form.Action.Submit

      您还可以返回自定义错误,这将帮助您处理您在代码中使用的不同状态。

      【讨论】:

      • 它不起作用,我的上传表单在我单击按钮时显示的窗口中,这可能是问题吗???
      • 否 - 只要处理服务器端上传的 PHP 脚本只返回 JSON,就足够了(即 PHP 应该返回的唯一内容是,即 '{success: true}' .