我正在使用这些方法上传文件,
客户端:
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 :(得分:0)
当您上传文件时,您实际上是在创建一个包含重复内容的新文件。
从你的代码:
using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py")))
{
Save(Context.Request.InputStream, Stream);
}
File.Create
负责新日期。
有关保留日期的信息,请参阅此answer。