2011-11-27 145 views

回答

3

當您使用一個文件輸入上傳文件時,此信息不會被髮送到服務器。只有文件名,MIME類型和內容與multipart/form-data一起發送。上傳之前,您可以使用HTML5 File API從文件中獲取此信息。


如這裏的註釋部分要求是它有一個上傳控件和一個隱藏字段將被用於存儲和使用HTML5發送文件最後修改日期到服務器的ASP.NET頁面的例子文件API:

<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Globalization" %> 

<script type="text/C#" runat="server"> 
    protected void BtnUploadClick(object sender, EventArgs e) 
    { 
     var file = Request.Files[0]; 
     DateTime date; 
     if (DateTime.TryParseExact(lastModifiedDate.Value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) 
     { 
      // you could use the date here 
     } 
    } 
</script> 

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <form id="Form1" runat="server"> 
     <label for="up">Pick a file</label> 
     <asp:FileUpload ID="up" runat="server" /> 
     <asp:HiddenField ID="lastModifiedDate" runat="server" /> 
     <asp:LinkButton ID="btnUpload" runat="server" Text="Upload" OnClick="BtnUploadClick" /> 
    </form> 

    <script type="text/javascript"> 
     if (!window.File) { 
      alert('Sorry, your browser doesn\'t support the File API so last modified date will not be available'); 
     } else { 
      document.getElementById('<%= up.ClientID %>').onchange = function() { 
       if (this.files.length > 0) { 
        if (typeof this.files[0].lastModifiedDate === 'undefined') { 
         alert('Sorry, your browser doesn\'t support the lastModifiedDate property so last modified date will not be available'); 
        } else { 
         var lmDate = this.files[0].lastModifiedDate; 
         var hidden = document.getElementById('<%= lastModifiedDate.ClientID %>'); 
         hidden.value = lmDate.getFullYear() + '-' + (lmDate.getMonth() + 1) + '-' + lmDate.getDate(); 
        } 
       } 
      }; 
     } 
    </script> 
</body> 
</html> 

因此,在這個例子中,我們認購onchange事件的文件輸入,如果客戶端瀏覽器支持HTML5文件API,我們可以獲取有關選定文件的信息,如名稱,大小,最後一次修改日期,...在這個例子中,我們將最後修改的日期存儲到隱藏字段中,以便此信息可用一旦我們上傳文件,就可以在服務器上運行。

+0

hmmmm,那麼如何解決這個問題呢? –

+1

@just_name,如果您可以支持HTML5,那麼您可以使用File API。如果沒有,您基本上無法做到這一點,因爲您無法訪問客戶端計算機上的這些信息。您將不得不添加另一個輸入字段,用戶可以手動輸入最後修改日期。 –

+0

@LesterDove的代碼呢? –

0

System.IO.FileInfo對象應該產生LastWriteTime財產

FileInfo myFileInfo= new FileInfo(path) ; 
myFileInfo.Refresh(); 
string t = myFileInfo.LastWriteTime.ToString("F") 
+0

不,這是'lastwritetime'不是'最後修改時間' –

相關問題