2011-12-30 65 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);