【发布时间】:2015-05-06 03:25:16
【问题描述】:
我看到了一些类似于我的问题的答案,但仍然无法弄清楚。
我正在使用下面的代码让用户上传 MP3 文件(我使用的是 FTP),它在本地主机(简单的 WinForm 应用程序)上运行良好,但在使用远程服务器(远程 DNN 站点)时抛出错误:
System.IO.FileNotFoundException: 找不到文件 'C:\Windows\SysWOW64\inetsrv\Test.mp3'。
我知道如果test.mp3 文件位于此服务器位置,那么它应该可以工作,但它实际上位于我的C:\Temp\Test.mp3 路径中。我认为FileUpload1 没有给出正确的文件路径。我该如何解决这个问题?
protected void btnUpload_Click(object sender, EventArgs e)
{
string url = System.Configuration.ConfigurationManager.AppSettings["FTPUrl"].ToString();
string username = System.Configuration.ConfigurationManager.AppSettings["FTPUserName"].ToString();
string password = System.Configuration.ConfigurationManager.AppSettings["FTPPassWord"].ToString();
string filePath = FileUpload1.PostedFile.FileName;
if (filePath != String.Empty)
UploadFileToFtp(url, filePath, username, password);
}
public static void UploadFileToFtp(string url, string filePath, string username, string password)
{
var fileName = Path.GetFileName(filePath);
var request = (FtpWebRequest)WebRequest.Create(url + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
using (var fileStream = File.OpenRead(filePath))
{
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
requestStream.Close();
}
}
var response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload done: {0}", response.StatusDescription);
response.Close();
}
【问题讨论】:
-
您尝试做的事情非常令人困惑...您是尝试将文件从服务器上传到其他 FTP 服务器,还是尝试通过您的 IIS 服务器从浏览器上传文件到外部 FTP 服务器?
-
旁注:“asp-classic”不太可能是您帖子的正确标签。可能是“ASP.Net”+“WebForms”——如果是这样,请编辑。
标签: c# asp.net upload ftp dotnetnuke