FTP文件上传到ASP页面

问题描述 投票:0回答:1

我看到一些类似于我的问题的答案,但仍然无法弄清楚。

我使用下面的代码供用户上传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();
}
c# asp.net upload ftp dotnetnuke
1个回答
0
投票

HttpPostedFile.FileName客户端上文件的完全限定名称”

我相信大多数Web浏览器实际上只提供文件名,没有任何路径。 所以你只得到Test.mp3 ,当你尝试在服务器上本地使用这样的“相对”路径时,它会被解析为Web服务器的当前工作目录,什么是C:\\Windows\\SysWOW64\\inetsrv

而是使用HttpPostedFile.InputStream直接访问上载的内容(将其复制到GetRequestStream )。

请参阅HttpPostedFile文档

© www.soinside.com 2019 - 2024. All rights reserved.