2012-01-09 29 views
0

我知道如何創建瀏覽和選擇文件的表單,這不是我的問題。我需要的是獲取選定文件的內容,將其發送到服務器並進行處理。現在我只能得到文件位置。ExtJS4文件上傳 - 如何讓我的文件內容不只是位置

我認爲如果我在客戶端(extjs)上獲取我的文件,然後將其發送到服務器會更好,但我不知道如何執行此操作。

{ 
    xtype: 'fileuploadfield', 
    hideLabel: true, 
    emptyText: 'Select a file to upload...', 
    id: 'upfile', 
    //name:'file', 
    width: 220 
}, 
buttons: 
[ 
    { 
     text: 'Upload', 
     handler: function() { 
      obj.Import(Ext.getCmp('upfile').getValue()) 
     } 
    } 
] 

Import(...)是我的服務器功能。我需要給它的文件不僅是它的路徑!

預先感謝您的寶貴時間

+0

導入是我的服務器功能? ExtJS代碼中的服務器端代碼如何? – 2012-01-09 10:53:30

+0

它包含代碼來處理文件,但我首先需要給它的文件,我不知道該怎麼做! – Armance 2012-01-09 11:39:47

回答

0

你在做什麼Ext.getCmp('upfile').getValue()顯然是將文件位置傳遞給Import方法。你如何期待文件內容呢?爲了上傳文件,您需要提交ExtJS表單。你可以做這樣的事情:

handler: function() { 

     form.submit({ 
       url: '/Gallery/Upload', 
       waitMsg: 'Uploading your file...', 
       success: function(form, action) { 
        alert('File form done!'); 
       } 
      }); 
     } 
    } 

而在服務器端:

public void ProcessRequest(HttpContext context) 
{ 
    HttpPostedFile fileupload = context.Request.Files["file"]; 

    // process your fileupload... 

    context.Response.ContentType = "text/plain"; 
    context.Response.Write("Ok"); 
} 

更新:你必須解除你的fileuploadfield的name屬性。

+0

thx ..什麼是AcceptVerbs(HttpVerbs.Post)?我的.ashx不能識別它 – Armance 2012-01-09 11:34:43

+0

我發佈的代碼是來自MVC控制器..檢查更新的代碼 – 2012-01-09 11:55:49

+0

它適用於我,非常感謝,但我想顯示消息錯誤,所以我返回一個字符串,問題是,當我使用context.Response.ContentType =「text/plain」; context.Response.Write(mymessage);它總是會觸發extjs中的成功方法,當我不使用它時,我得到任何消息:( – Armance 2012-01-16 18:04:03

5

AFAIK分機不使用HTML5文件API,因此讓在一邊JS文件內容並不簡單。可能最簡單的方法是創建自定義處理程序。例如:

{ 
    xtype: 'fileuploadfield', 
    hideLabel: true, 
    emptyText: 'Select a file to upload...', 
    id: 'upfile', 
    //name:'file', 
    width: 220 
}, 
buttons: 
[ 
    { 
     text: 'Upload', 
     handler: function() { 
      var file = Ext.getCmp('upfile').getEl().down('input[type=file]').dom.files[0]; // fibasic is fileuploadfield 
      var reader = new FileReader(); 
      reader.onload = (function(theFile) { 
       return function(e) { 
        obj.Import(e.target.result); 
       }; 
      })(file); 
      reader.readAsBinaryString(file); 
     } 
    } 
] 
+0

thx..im試試看,read.readAsBinaryString(f)中的f是什麼; ?你的意思是文件? – Armance 2012-01-09 11:46:35

+2

@astrocybernaute是的,我的壞。順便說一句[這裏](http://www.html5rocks.com/en/tutorials/file/dndfiles/)是很好的教程關於HTML 5文件API – Krzysztof 2012-01-09 11:50:06

+0

另一個問題PLZ ..什麼是e.target.result的類型? (在導入函數中聲明)ty – Armance 2012-01-09 11:57:02