将文件上传到远程服务器 [英] Upload files to a remote server
问题描述
我需要将Web服务器中的asp.net(C#)页面上的文件上传到远程服务器.
I need to upload files from my asp.net (C#) page residing in the web server to a remote server.
我设法使用以下代码从本地主机将文件上传到远程服务器:
I managed to upload files to remote server from localhost using this code:
string serverPath = "\\\\xx.xxx.xx.xx\\Folder\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(serverPath);
但是当我将此代码发布到Web服务器后,它停止工作并显示错误找不到网络路径."
But after I published this code to my web server, it stopped working with the error "The network path was not found."
我研究了一些建议使用UNC网络共享并实现模拟的解决方案.我不知道如何应用这些解决方案.
I have looked at a few solutions which suggest using UNC network share and implementing impersonation. I couldn't figure out how to apply these solutions.
有人可以举一个例子,或者提出一个更简单的解决方案.
Can someone please give an example, or suggest a simpler solution.
谢谢!
推荐答案
在FileUpload1.PostedFile.SaveAs(path)中,path是文件的物理路径,无网址.您必须检查:
In FileUpload1.PostedFile.SaveAs(path), path is physical path of file, No Url. You must check:
- 物理文件夹是否存在?
- 您有权访问文件夹吗?
如果两个问题的答案都正确,请检查以下代码:
if answer of both question is true check this code:
string serverPath = @"\\xxx.xxx.xxx.xxx\Folder\";
if (!System.IO.Directory.Exists(serverPath))
System.IO.Directory.CreateDirectory(serverPath);
FileUpload1.PostedFile.SaveAs(serverPath + FileUpload1.FileName);
这篇关于将文件上传到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!