![](/img/trans.png)
[英]How to get file name from asp:FileUpload and display in a Label using ASP.NET and C#
[英]How to remove a file moved with a FileUpLoad [asp.net] C#
我有一个加载,处理和移动文件CSV的过程。 此行为已由2种方式表示,但我在下面描述的第2种方法中遇到问题:
源目标和目标目录在数据库中设置。 因此读取和移动文件的工作原理很好,从源目录中消失了副本。 (好)
源目录是从FileUpload控件(fuControl.PostedFile.FileName)获取的,目标仍在数据库中设置。 因此,读取并移动文件,但此时,我看到文件被复制到目标目录,但锁定的副本仍保留在源目录中,只有在刷新页面(F5)或退出Internet浏览器时才会消失。 (不好)那么,我该如何避免这种情况?
这是我的示例代码:
private void RunProcess(string path)
{
try
{
using (StreamReader stream = File.OpenText(path))
{
this.ProcessFile(stream);
}
string destination = this.GetDestinationPath(); //Get the path from DB
string fileName = Path.GetFileName(path);
File.Move(path, destination + fileName);
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
- - -编辑 - - -
我在转发器中有我的FileUpload控件,我就像这样使用它。 它不起作用,副本仍然存在。
protected void repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Browse")
{
using (FileUpload fuSource = (FileUpload)e.Item.FindControl("fuSource"))
{
if (fuSource != null)
{
if (String.IsNullOrEmpty(fuSource.FileName) == false)
{
string filePath = fuSource.PostedFile.FileName;
this.RunProcess(filePath);
}
}
}
}
}
-----编辑2 -----
public void ProcessFile(StreamReader stream)
{
string line = String.Empty;
while ((line = stream.ReadLine()) != null)
{
//Just 1 line.
Console.WriteLine(line);
}
}
FileUpload是可处置的,因此它应该包含在using语句中。 这应该释放在Web应用程序超时之前保留的任何文件锁。
using(var fileUploadControl = new FileUpload())//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.