【发布时间】:2013-08-08 15:49:33
【问题描述】:
我有一个 ASP.NET Web 表单网站。当我按下上传按钮时,此代码运行。这段代码有很多问题。它不是快速和简单的,它有一个大问题。第一次上传图片,效果很好,但是当我想替换文件时返回Access denied 错误。我在 IIS express 上遇到此错误,但它在 Visual Studio Development Server 上正常工作。
注意:我有一个网格视图,显示必须更改哪个用户图像;这个网格视图还可以帮助我找到特定的用户图像文件。
if (UsersImageFileUpload.HasFile && UsersImageFileUpload.PostedFile.ContentType.StartsWith("image"))
{
string filename = UploadUsersImageGridView.SelectedRow.Cells[1].Text.ToString();
if (!File.Exists(Server.MapPath("~/App_Data/Backups/" + filename)))
{
UsersImageFileUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data/Users/") + filename);
}
else if (File.Exists(Server.MapPath("~/App_Data/Backups/" + filename)))
{
File.Delete(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
UsersImageFileUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
File.Replace(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"), Server.MapPath("~/App_Data/Users/") + filename, Server.MapPath("~/App_Data/Backups/"));
File.Delete(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
}
}
}
错误标题:Server Error in '/' Application。
错误第二个标题:The process cannot access the file 'D:\Website\App_Data\Users\1-fullname' because it is being used by another process.
错误说明:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
错误详情:System.IO.IOException: The process cannot access the file 'D:\App_Data\Users\1-fullname' because it is being used by another process
我的问题:
- 我的代码有什么问题?
- 为什么 IIS 返回该错误(拒绝访问文件)?
- IIS Express 8 和 Visual Studio 2012 开发服务器之间最重要的区别是什么?
- 有更快的创建上传和替换的方法吗?
- 有没有更好的形式来拥有这个系统?
【问题讨论】:
-
运行应用程序池的帐户需要具有读取/写入该文件的权限 - 这是我首先要看的地方。
-
检查您是否有权访问临时 Internet 文件位置
-
此代码在
localhost上也有问题。这不仅仅是我服务器的问题。 -
这不是权限问题。您要使用的文件在您要访问时正在使用中。找到正在使用该文件的进程并将其杀死。该主题可能对您有所帮助:stackoverflow.com/questions/317071/…
标签: asp.net