问题描述
有一个 IP 为 192.168.0.36
的端口。这是一个将存储上传到 Web 应用程序的文件的服务器,但这是我第一次尝试做类似的事情,所以我尝试了很多方法来将文件存储在该服务器中,但是当它发送文件时应用程序生成以下错误:
WebClient 请求期间的异常
所以我打开了 Visual Studio 错误菜单以了解更多信息,并且有一条消息说:
InnerException = {"找不到文件'C:\Program Files (x86)\IIS Express\download.png'。":"C:\Program Files(x86)\IIS Express\download.png" }
如果我没有访问分区 C,为什么会产生这个错误:
这是我的代码:
<asp:FileUpload runat="server" ID="file_foto" CssClass="file-foto-pf" Style="display: none;" accept="image/*" />
protected void actualizar_foto(object sender,EventArgs e)
{
try
{
if (file_foto.HasFile)
{
string Id_Usuario = Request.QueryString["Id_Usuario"].ToString();
string serverName = @"\\192.168.0.36\\E:\\INTRANET\\";
string folder = "PRUE\\";
string fileName = Path.GetFileName(file_foto.FileName)
// Upload to Hosted Server.
WebClient webClient = new WebClient();
Uri uri = new Uri(serverName + folder + fileName);
webClient.UploadFile(uri,"POST",fileName)
DCL.Int_Usuarios obj = new DCL.Int_Usuarios();
obj.Anexo_Foto = uri.ToString();
obj.Id_Usuario = Convert.ToInt32(Id_Usuario);
Int_Usuarios_BRL.InsertOrUpdate(obj,45);
}
Page.Response.Redirect(Page.Request.Url.ToString(),true);
}
catch (Exception ex)
{
Console.Write(ex);
}
}
解决方法
虽然我不确定这是否是问题所在,但您可以执行以下操作:
string serverName = @"file://192.168.0.36/E:/INTRANET/PRUE/";
string fileName = Path.GetFileName(file_foto.FileName);
Uri uri = new Uri(Path.Combine(serverName,fileName));
如果这对您有帮助,请告诉我。