【发布时间】:2010-10-24 17:26:26
【问题描述】:
是否可以使用 ASP.NET 将文件直接上传到 ftp 帐户文件夹中?
例如我点击浏览,选择要上传的文件,当我点击“上传”按钮时,它应该将其直接保存到另一个 Web 服务器上的文件夹中,该文件夹位于其他地方,而不是用于上传的服务器。
【问题讨论】:
标签: asp.net upload ftp file-upload
是否可以使用 ASP.NET 将文件直接上传到 ftp 帐户文件夹中?
例如我点击浏览,选择要上传的文件,当我点击“上传”按钮时,它应该将其直接保存到另一个 Web 服务器上的文件夹中,该文件夹位于其他地方,而不是用于上传的服务器。
【问题讨论】:
标签: asp.net upload ftp file-upload
/// <summary>
/// Example call : if (FtpUpload(FileUploadControl1, "ftp.my.com/somePathDir", @"user", "pass!", "domain"))
/// </summary>
/// <param name="file"></param>
/// <param name="ftpServer"></param>
/// <param name="username"></param>
/// <param name="ftpPass"></param>
/// <returns></returns>
private bool FtpUpload(FileUpload file, string ftpServer, string username, string ftpPass, string domainName = "")
{
// ftp://domain\user:password@ftpserver/url-path
// If you are a member of a domain, then "ftp://domain-name\username:password@url-path" may fail because the backslash (\) is sent in as a literal character and Internet Explorer incorrectly looks for a file instead of parsing a Web address. Changing the backslash (\) in the domain-name\username to domainname%5Cusername works correctly.
try
{
string ftpAddres;
if (domainName != string.Empty)
ftpAddres = "ftp://" + domainName + @"%5C" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName;
else
ftpAddres = "ftp://" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName;
using (var webClient = new System.Net.WebClient())
{
webClient.UploadData(new Uri(ftpAddres), file.FileBytes);
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return true;
}
【讨论】:
您可以使用WebClient 类将上传的文件存储到FTP(无需将其作为文件保存在服务器上)。像这样的:
string name = Path.GetFileName(UploadControl.FileName);
byte[] data = UploadControl.FileBytes;
using (WebClient client = new WebClient()) {
client.UploadData("ftp://my.ftp.server.com/myfolder/" + name, data);
}
【讨论】:
据我了解您的问题,您想将文件上传到另一台远程服务器(因此它不是与您的网络服务器位于同一网络上的另一台服务器)?在这种情况下,您可以做几件不同的事情。最简单的方法可能是首先将常规文件上传到您的服务器,然后让您的服务器通过 FTP 将文件发送到另一个远程服务器:
string fileName = Path.Combine("<path on your server", FileUpload1.FileName);
FileUpload1.SaveAs(fileName);
using(System.Net.WebClient webClient = new System.Net.WebClient())
{
webClient.UploadFile(
New Uri("ftp://remoteserver/remotepath/" + FileUpload1.FileName),
localFile);
}
...或者它可以一步完成:
using(System.Net.WebClient webClient = new System.Net.WebClient())
{
webClient.UploadData(
New Uri("ftp://remoteserver/remotepath/" + FileUpload1.FileName),
FileUpload1.FileBytes);
}
(我没有尝试过这段代码,所以其中可能有一些错误......)
更新:我注意到我错误地假设 WebClient 的 UploadXXX 方法是静态的...
【讨论】:
编辑
首先,@ 符号用于将字符串标记为文字。它使您不必转义反斜杠等字符。例如
string path = "Z:\\Path\\To\\File.txt";
string path = @"Z:\Path\To\File.txt";
其次,如果您只有 FTP 访问其他服务器,那么您可以使用 FileUpload 控件的FileUpload.FileBytes 属性。这将为您提供文件内容的byte[]。
从此您可以使用System.Net.FtpWebRequest 和System.Net.FtpWebResponse 将您的文件上传到FTP 帐户。
VB.NET 中有一些示例代码,但应该很容易理解
http://www.programmingforums.org/thread15954.html
ORIG
文件上传控件将为您提供网络服务器上的文件。
由您决定是否将该文件从网络服务器复制/保存到任何服务器 你的 FTP 托管在上面。
您是否有在其他服务器上共享的 UNC 路径/映射驱动器可以保存到。
FileUpload 控件有一个.SaveAs() 方法,所以很简单
if (FileUpload1.HasFile)
try
{
FileUpload1.SaveAs(@"Z:\Path\On\Other\Server\" + FileUpload1.FileName);
}
【讨论】:
您不能直接从您的 HTML 表单将其上传到 FTP。但是,您可以将其上传到您的 ASP.NET 应用程序,然后使用 FtpWebRequest 从那里将其上传到 FTP。
【讨论】: