2013-03-20 15 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

相关问题