【发布时间】:2012-05-20 07:00:05
【问题描述】:
我有他下面的代码
HttpPostedFileBase files = Request.Files[0];
Stream fileStream = files.InputStream;
using (Stream file = System.IO.File.OpenWrite(originalImage))
{
StreamUtil.CopyStream(fileStream, file);
file.Close();
fileStream.close();
fileStream.dispose();
}
// here is CopyStream method
public static void CopyStream(Stream input, Stream output)
{
var buffer = new byte[8*1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
当我尝试两次写入同一个文件时,我得到了
The process cannot access the file \u0027D:FILENAME because it is being used by another process
我怎样才能关闭它?所以一旦请求写入完成,它将被关闭?
【问题讨论】:
-
什么是
fileStream?它来自哪里? -
更新了代码,它来自请求。
-
在您尝试再次写入同一个文件时每次都会发生这种情况吗?您是否尝试使用filemon 查看锁定文件的进程?
-
是的,每次我上传同一个文件时都会发生这种情况。
标签: c# asp.net-mvc-3 file stream