2013-03-20 50 views
0

我使用這些方法來上傳文件,Silverlight的文件上傳與原始的創建,修改和訪問日期

客戶端:

private void Add(object sender, MouseButtonEventArgs e) 
    { 
     OpenFileDialog OFD = new OpenFileDialog(); 
     OFD.Multiselect = true; 
     OFD.Filter = "Python (*.py)|*.py"; 
     bool? Result = OFD.ShowDialog(); 
     if (Result != null && Result == true) 
      foreach (var File in OFD.Files) 
       mylistbox.Items.Add(File); 
    } 
    private void Submit(object sender, MouseButtonEventArgs e) 
    { 
     foreach (var File in mylistbox.Items) 
      Process(((FileInfo)File).Name.Replace(((FileInfo)File).Extension, string.Empty), ((FileInfo)File).OpenRead()); 
    } 
    private void Process(string File, Stream Data) 
    { 
     UriBuilder Endpoint = new UriBuilder("http://localhost:5180/Endpoint.ashx"); 
     Endpoint.Query = string.Format("File={0}", File); 

     WebClient Client = new WebClient(); 
     Client.OpenWriteCompleted += (sender, e) => 
     { 
      Send(Data, e.Result); 
      e.Result.Close(); 
      Data.Close(); 
     }; 
     Client.OpenWriteAsync(Endpoint.Uri); 
    } 
    private void Send(Stream Input, Stream Output) 
    { 
     byte[] Buffer = new byte[4096]; 
     int Flag; 

     while ((Flag = Input.Read(Buffer, 0, Buffer.Length)) != 0) 
     { 
      Output.Write(Buffer, 0, Flag); 
     } 
    } 

服務器端:

public void ProcessRequest(HttpContext Context) 
    { 
     using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py"))) 
     { 
      Save(Context.Request.InputStream, Stream); 
     } 
    } 

    private void Save(Stream Input, FileStream Output) 
    { 
     byte[] Buffer = new byte[4096]; 
     int Flag; 
     while ((Flag = Input.Read(Buffer, 0, Buffer.Length)) != 0) 
     { 
      Output.Write(Buffer, 0, Flag); 
     } 
    } 

我的問題是上傳的文件有不同的創建,修改&訪問日期
爲什麼?

+0

你能給一個日期的例子嗎?與上傳的原始文件不同嗎? – 2013-03-20 16:59:06

+0

原來的日期被今天的日期取代。 – 2013-03-21 12:38:03

+0

這是因爲您正在創建一個新文件。我以爲你的意思是新文件上的創建日期與新文件上的修改日期不同。當你上傳一個文件時,你實質上是在創建一個新文件,其中包含重複的內容。 – 2013-03-21 13:59:54

回答

0

當你上傳一個文件時,你基本上是在創建一個新文件,並且有重複的內容。

從代碼:

using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py"))) 
{ 
    Save(Context.Request.InputStream, Stream); 
} 

File.Create負責新日期。

有關保存日期的信息,請參閱此answer

+0

「但是這個解決方案只適用於圖像文件」 – 2013-03-22 20:19:25

相關問題