Asp.net mvc 2 登录时上载文件时出错
我有一个服务器主机,它只允许我通过ftp上传文件(我的应用程序中有代码)。 所以我得到了一个代码,可以让我上传文件到服务器上,除了一件奇怪的事情,如果我/用户登录(认证),我就不能上传文件。这是在黑暗中拍摄的,看看是否有人知道这是为什么 我的错误信息是Asp.net mvc 2 登录时上载文件时出错,asp.net-mvc-2,authentication,iis-7,Asp.net Mvc 2,Authentication,Iis 7,我有一个服务器主机,它只允许我通过ftp上传文件(我的应用程序中有代码)。 所以我得到了一个代码,可以让我上传文件到服务器上,除了一件奇怪的事情,如果我/用户登录(认证),我就不能上传文件。这是在黑暗中拍摄的,看看是否有人知道这是为什么 我的错误信息是 Access to the path 'D:\hshome\PATH' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at Sy
Access to the path 'D:\hshome\PATH' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj) at System.IO.Directory.CreateDirectory(String path) at BasicProject.Mvc.Areas.Account.Controllers.ProfileController.Test(TestViewModel viewModel)
我上传时使用的代码如下
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item];
if (file != null)
{
var path = "File";
var filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path + "/");
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://SITE/SITE/" + path + "/" + file.FileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("USER", "PASSWORD");
request.UsePassive = false;
var sourceStream = new StreamReader(file.InputStream);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
就像我说的,如果我不登录,这是一种冒险,如果你有同样的问题,我会非常高兴地得到一个解决方案。当你登录时,IIS试图模拟你,你没有访问该文件夹的权限,只有IIS用户有。
调试代码的几个选项。我对远程服务器的偏好是在文件中的每个关键位置之前添加Response.write,例如在
System.Web.HttpContext.Current.server.MapPath(“~/”+path+“/”)之后
将告诉您是否已完成该步骤,并可能(如果已设置)有关请求的信息,如当前安全上下文/用户
例如:
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item];
if (file != null)
{
Response.Write("A");
var path = "File";
var filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path + "/");
Response.Write("B");
if (!Directory.Exists(filePath))
{
Response.Write("C");
Directory.CreateDirectory(filePath);
}
Response.Write("D");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://SITE/SITE/" + path + "/" + file.FileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("USER", "PASSWORD");
request.UsePassive = false;
var sourceStream = new StreamReader(file.InputStream);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
如果您从这个开始,看到输出ABCD,那么您就知道问题还在后面,所以只需重复这个过程。(我发现一次添加太多调试行会让人分心,但您可以这样做。)如果您停在,说“ABC”,那么您知道
Directory.CreateDirectory(filePath)代码>失败。当您登录和未登录时,您可以尝试执行Response.Write(HttpContext.Current.User.Identity)
,看看您得到了什么。(我不确定这是您想要的属性,但我现在无法测试它,因此您可以尝试查找它或只是进行实验。)问题是,我无法在进行身份验证时创建文件夹,因此在这种特定情况下,我会在用户注册时为他们创建文件夹,使用上面显示的ftp上传代码,效果很好。我能做些什么来解决这个问题?我试图在我的web.config中模拟,但我得到一个错误,说我不能在那里有它。我应该如何响应。Write()?您可能不希望模拟,您可能希望以应用程序池用户的身份写入文件(这可能因您的主机提供商而异,我对此一无所知,主要是因为您没有提及)。您是否尝试问主机是否知道发生了什么?