2011-12-30 61 views
0

我想要做的是使用用户输入更改文件并将更新后的文件上载回用户。在服务器上更改文件后将文件上传到用户

现在我有一些Java脚本,从输入收集数据:

$('#collectButton').click(function() { 
    var inputs = $('#someDiv input').get(); 

因为我收集我需要将其发送给我的PHP代码数据,它是由阿贾克斯邮政和JSON(阵列转移)完成。

$.ajax({ 
     type: "POST", 
     url: "Some.php", 
     data: {postedData:JSON.stringify(inputs)}, 
    success: function() 
      { 
     alert('done!');        
      } 
    }); 

Firebug的控制台确认数据传输和来这里的问题:

对此我看到了改变的文件(TXT简单),这应该是上传回来,但事实并非如此。

PHP,我用的是:

if (isset ($_POST['postedData'])){ 
     $changes = (json_decode($_POST['postedData'])); 
    .... 
    some changes done by for loop 
    .... 

     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename='.basename($nwFile)); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($nwFile)); 
     ob_clean(); 
     flush(); 
     readfile($nwFile); 
     // deletes file right after 
     fclose($nwFile); 
     unlink($nwFile); 

如何去做正确的?

为什么PHP的“header”部分被忽略?

PS

独立的PHP文件工作格栅。

问题只出现在“调用PHP”

+0

您是否确认$ nwFile实际上是在服务器上创建的?另外,你为什么在头文件之后立即调用ob_clean()? – Yaniro 2011-12-30 20:31:36

+0

Yaniro - 是$ nwFile创建的 ob_clean() - 是一个问题吗?它正在清理输出缓冲区。我不需要缓冲区,文件中的所有数据。 – owner 2011-12-30 21:25:35

+0

尝试在标题前使用ob_clean(),但是你真的需要它吗? – Yaniro 2011-12-30 21:36:05

回答

0

下面是一个简单的例子:

<html> 
    <body> 
     <iframe name="fileDownload" style="position: absolute; visibility: hidden;"></iframe> 
     <form action="download.php" method="post" target="fileDownload"> 
      <input type="hidden" id="postedData" name="postedData" value="some data"> 
      <input type="submit"/> 
     </form> 
    </body> 
</html> 

请注意,我创建了一个隐藏的内嵌框架(iframe中)有一个明确的名称。 页面上的表单将一个名为postedData的参数提交给一个将在隐藏的iframe中执行的文件download.php。 download.php应取参数,生成文件并发送相应的头文件&内容。您还可以从iframe中删除style =“...”,并查看iframe中发生了什么,并在必要时进行调试。

可以设置postedData参数的内容,像这样:

function setData(data) 
{ 
    document.getElementById("postedData").value = data; 
} 

我已经在Chrome,Firefox 3.6的测试此,资源管理器8 &歌剧院和它工作正常。

+0

这不是我实际需要的。这是传输数据的直接方式。在将数据传输到PHP之前,我需要按照正确的顺序预先检查和预先设置数据,所以必须先使用JS。 – owner 2011-12-31 14:57:13

+0

“按正确顺序预检查和预设数据”是什么意思? – Yaniro 2011-12-31 16:18:26

0

我用这个网站的脚本http://tutorialzine.com/2011/05/generating-files-javascript-php/,改编了一下,它对我来说工作得很好。

$(document).ready(function(){ 

     $('#download').click(function(e){ 

      $.generateFile({ 
       filename : 'export.txt', 
       content  : $('textarea').val(), 
       script  : 'download.php' 
      }); 

      e.preventDefault(); 
     });} 

(function($){ 

    // Creating a jQuery plugin: 

     $.generateFile = function(options){ 

      options = options || {}; 

      if(!options.script || !options.filename || !options.content){ 
       throw new Error("Please enter all the required config options!"); 
      } 

      // Creating a 1 by 1 px invisible iframe: 

      var iframe = $('<iframe>',{ 
       width:1, 
       height:1, 
       frameborder:0, 
       css:{ 
        display:'none' 
       } 
      }).appendTo('body'); 

      var formHTML = '<form action="" method="post">'+ 
       '<input type="hidden" name="filename" />'+ 
       '<input type="hidden" name="content" />'+ 
       '</form>'; 

      // Giving IE a chance to build the DOM in 
      // the iframe with a short timeout: 

      setTimeout(function(){ 

       // The body element of the iframe document: 

       var body = (iframe.prop('contentDocument') !== undefined) ? 
           iframe.prop('contentDocument').body : 
           iframe.prop('document').body; // IE 

       body = $(body); 

       // Adding the form to the body: 
       body.html(formHTML); 

       var form = body.find('form'); 

       form.attr('action',options.script); 
       form.find('input[name=filename]').val(options.filename); 
       form.find('input[name=content]').val(options.content); 

       // Submitting the form to download.php. This will 
       // cause the file download dialog box to appear. 

       form.submit(); 
      },50); 
     }; 

    })(jQuery);