2011-08-01 24 views
0

我想構建將處理某些文件的Web服務。如何上傳文件並顯示其信息

這是我想做的事:

  1. 用戶上傳使用「上傳表單」文件到服務器,文件保存爲在服務器端
  2. 服務器端的臨時文件Python腳本處理臨時文件併產生一些統計數據(例如,文件中的行和詞語的數量)
  3. 的統計信息顯示附近的「上傳表單」

這裏的問題是:我想要在背景剛剛上傳之後處理文件,並且在完成之後,.append()將生成的結果顯示到當前視圖。我不要想要分配一個腳本到<form action="processing_script.php">...因爲用戶將點擊上傳按鈕後重定向到processing_script.php

任何線索?也許有一些整潔的Ajax電話?

+1

這裏這個答案可能有點幫助:http://stackoverflow.com/questions/3080922/ajaxform-and-app-engine-blobstore/4334677#4334677 – sje397

回答

0

是的,你需要ajax。照常創建表單,然後使用Ajax提交。表格處理可以照常進行。

如果谷歌「文件上傳阿賈克斯」我敢肯定,你可以找到你需要:)

0

是啊,我犯了第二個Ajax請求,並與時間表(例如每10秒)運行它的一切。它會詢問服務器是否處理了上傳的文件。服務器甚至可以在外部程序中進行文件處理。接受第二個Ajax請求的php腳本會檢查一些READY狀態並給予客戶答案YES/NO/FAILED。當客戶接受YES答案時,它將用戶重定向到結果頁面。如果它接受NO,它會向用戶提示問題。

1
function ajaxRequest(){ 
    var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE 
    if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) 
    for (var i=0; i<activexmodes.length; i++){ 
    try{ 
    return new ActiveXObject(activexmodes[i]) 
    } 
    catch(e){ 
    //suppress error 
    } 
    } 
    } 
    else if (window.XMLHttpRequest) // if Mozilla, Safari etc 
    return new XMLHttpRequest() 
    else 
    return false 
} 

function postFile(){ 
var mypostrequest=new ajaxRequest() 
mypostrequest.onreadystatechange=function(){ 
    if (mypostrequest.readyState==4){ 
    if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){ 
    document.getElementById("my_Result_tag").innerHTML=mypostrequest.responseText //this is where the results will be put! 
    } 
    else{ 
    alert("An error has occured making the request") 
    } 
    } 
} 
var file = document.getElementById("my_file"); 
var parameters="file="+file //i am not sure of this peice though 
mypostrequest.open("POST", "basicform.php", true) 
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded") 
mypostrequest.send(parameters) 
} 
+0

豈不包裝所有的髒Ajax的東西解決兼容性問題? –

+0

是啊,它會..只是爲了示範 –

相關問題