C# 如何使用web客户端c写入另一个mvc应用程序中的文件夹
我希望有人能就我目前的情况向我提供建议。我有两个asp mvc应用程序,一个是桌面版,另一个是移动版。我希望能够将移动版本上传的文件写入桌面版本的文件夹中,因为后者包含应用程序的管理部分。我可以使用.Net的WebClient类将文件从一个下载到另一个,但我无法将文件从一个上传到另一个。我有一个控制台应用程序来测试上传,每当我尝试时,我都会收到一个错误,说对应用程序中文件夹的访问被拒绝。我尝试在App_数据文件夹之外的应用程序中创建一个文件夹,因为我认为该文件夹受到限制,但我得到了相同的错误。有人能告诉我该怎么办吗?我在谷歌上到处读到,这可以很容易地从IIS管理中解决,但我怀疑我的托管公司的人是否愿意授予我管理任何应用程序的任何权利。 我编写的代码只有在尝试保存文件时才会失败。以下是接收请求的站点的代码:C# 如何使用web客户端c写入另一个mvc应用程序中的文件夹,c#,asp.net-mvc,asp.net-mvc-4,uploading,C#,Asp.net Mvc,Asp.net Mvc 4,Uploading,我希望有人能就我目前的情况向我提供建议。我有两个asp mvc应用程序,一个是桌面版,另一个是移动版。我希望能够将移动版本上传的文件写入桌面版本的文件夹中,因为后者包含应用程序的管理部分。我可以使用.Net的WebClient类将文件从一个下载到另一个,但我无法将文件从一个上传到另一个。我有一个控制台应用程序来测试上传,每当我尝试时,我都会收到一个错误,说对应用程序中文件夹的访问被拒绝。我尝试在App_数据文件夹之外的应用程序中创建一个文件夹,因为我认为该文件夹受到限制,但我得到了相同的错误。有
[HttpPost]
[AllowAnonymous]
public string SubirArchivosRegistro(HttpPostedFileBase file)
{
string MensajeError = "";
string path = "";
string Success = " El (los) diplomas subidos exitosamente ";
try
{
if (file != null && file.ContentLength > 0)
{
path = Path.Combine(Server.MapPath("~/ArchivosRegistroUsuarios"), file.FileName);
file.SaveAs(path);
Success += file.FileName +",";
}
///return path;
return Success;
}
catch(Exception ex)
{
MensajeError = "Ha ocurrido un error, no se pudieron subir los archivos solicitados "+ex.Message;
return MensajeError;
}
}
这是客户端部分的代码没有一个代码失败,只是权限问题:
try
{
WebClient cliente = new WebClient();
if (GendarmeriaCertNombre != null && GendarmeriaCertNombre.ElementAt(0) != null)
{
gendarmeria = new List<Guid?>();
gendarmeriaNombre = new List<string>();
foreach (HttpPostedFileBase file in GendarmeriaCertNombre)
{
if (file != null)
{
string filename = Path.GetFileName(file.FileName);
Guid guidnum = _fileStore.SaveUploadedFile(file, "gendarmeria");
gendarmeria.Add(guidnum);
gendarmeriaNombre.Add(file.FileName);
string ruta = _fileStore.GetDiskLocation(guidnum);
byte[] respuesta = cliente.UploadFile("http://localhost/DiplomadoEnMandoPolicial/Account/SubirArchivosRegistro", "POST", ruta);
response = System.Text.Encoding.ASCII.GetString(respuesta);
}
}
if (gendarmeria != null && gendarmeria.Count > 0 && gendarmeriaNombre != null && gendarmeriaNombre.Count > 0)
{
registro.GendarmeriaCertificado = String.Join(",", gendarmeria.Select(t => t.Value.ToString("D")));
registro.GendarmeriaCertNombre = String.Join(",", gendarmeriaNombre);
}
}
if (CursoInacipeCertNombre != null && CursoInacipeCertNombre.ElementAt(0) != null)
{
inacipe = new List<Guid?>();
inacipeNombre = new List<string>();
foreach (HttpPostedFileBase file in CursoInacipeCertNombre)
{
if (file != null)
{
string filename = Path.GetFileName(file.FileName);
Guid guidnum = _fileStore.SaveUploadedFile(file, "inacipe");
inacipe.Add(guidnum);
inacipeNombre.Add(file.FileName);
string ruta = _fileStore.GetDiskLocation(guidnum);
byte[] respuesta = cliente.UploadFile("http://localhost/DiplomadoEnMandoPolicial/Account/SubirArchivosRegistro", "POST", ruta);
response = System.Text.Encoding.ASCII.GetString(respuesta);
}
}
if (inacipe != null && inacipe.Count > 0 && inacipeNombre != null && inacipeNombre.Count > 0)
{
registro.CursoInacipeCertificado = String.Join(",", inacipe.Select(t => t.Value.ToString("D")));
registro.CursoInacipeCertNombre = String.Join(",", inacipeNombre);
}
}
if (CursoCideCertNombre != null && CursoCideCertNombre.ElementAt(0) != null)
{
cide = new List<Guid?>();
cideNombre = new List<string>();
foreach (HttpPostedFileBase file in CursoCideCertNombre)
{
if (file != null)
{
string filename = Path.GetFileName(file.FileName);
Guid guidnum = _fileStore.SaveUploadedFile(file, "cide");
cide.Add(guidnum);
cideNombre.Add(file.FileName);
string ruta = _fileStore.GetDiskLocation(guidnum);
byte[] respuesta = cliente.UploadFile("http://localhost/DiplomadoEnMandoPolicial/Account/SubirArchivosRegistro", "POST", ruta);
response = System.Text.Encoding.ASCII.GetString(respuesta);
}
}
if (cide != null && cide.Count > 0 && cideNombre != null && cideNombre.Count > 0)
{
registro.CursoCideCertificado = String.Join(",", cide.Select(t => t.Value.ToString("D")));
registro.CursoCideCertNombre = String.Join(",", cideNombre);
}
cliente.Dispose();
}
}
这就是问题所在,我愿意接受任何建议 测试1:首先检查您是否可以上传到同一个应用程序,看看您是否可以这样做?这两个应用程序很可能在两个不同的应用程序池标识下运行。通常托管公司会采用这种方式,这样每个web应用程序都可以单独管理,而不会相互干扰。如果Test1正常,您可能必须为app 1的应用程序池标识授予适当的权限,才能将文件写入app 2。请看这条线@Prashanth Thurairatnam抱歉,我没有完全理解你。测试1是测试我是否可以从第一个不使用WebClient的应用程序上传文件。我检查过了,没问题。否则我想这是IIS的问题,正如你所说的。谢谢你的评论。我会告诉你事情的进展。