English 中文(简体)
带有 jqgrid 和 ajax 文件加载的 dilemmna
原标题:dilemna with jqgrid and ajaxfileupload
  • 时间:2012-05-25 15:35:57
  •  标签:
  • ajax
  • jqgrid

我使用 jqgrid 和 ajaxFileUpload.js 脚本将参数和文件传送到php 脚本。 代码的结构是这样 :

 ...  
 url:url_1.php,  
 beforeSubmit: function (postdata,formid)  
   {  
      $.ajaxFileUpload (  
         {  
             url: url_2.php,  
             ...  
             success:  
             error:    
         }), 
      return[true,""];  
   }, 
afterSubmit: function(reponse,postdata)  
   {
      ...  
      return [true,  ,  ];
   }  

I have a dilemna:
According to the jqgrid behaviour, url_2.php is called, then url_1.php.
url_2.php handles the data (parameters + file), url_1.php handles nothing.
url_2.php could return an error or message (e.g "already exist") but, the errors are displayed in the form by the aftersubmit event, and this event receives error from url_1.php !!!
I suppose that I am obliged to put the ajaxfileupload in the beforesubmit event !!!
Any ideas to solve this dilemna ?

问题回答

您可以使用jquery 窗体插件和 jqGrid 数据Proxy 方法代替 。

    useDataProxy: true,
    dataProxy :  function (opts, act) {  

    opts.iframe = true;
    var $form = $( #FrmGrid_  + $grid.jqGrid( getGridParam ,  id ));
    //Prevent non-file inputs double serialization
    var ele = $form.find( INPUT,TEXTAREA,SELECT ).not( :file );
    ele.each(function () {
        $(this).data( name , $(this).attr( name ));
        $(this).removeAttr( name );
    });

    //Send only previously generated data + files
    $form.ajaxSubmit(opts);
    //Set names back after form being submitted
    setTimeout(function () {
        ele.each(function () {
            $(this).attr( name , $(this).data( name ));
        });
    }, 200);
};

例如,