C# 将文件上载到FTP服务器在本地主机上工作,但在部署时不工作
我花了一整天的时间想弄明白这一点,但运气不好 基本上,我有一堆文件和文件夹,我想上传到FTP服务器上,所有的东西都可以在本地主机上运行。但当我将它部署到azure时,它就停止工作了。在我看来,这毫无意义,我最初的想法是它与防火墙、上传超时或其他东西有关 您是否看到我的代码中缺少什么,或者我是否需要在azure或ftp服务器上进行一些配置 编辑:我忘记包含错误消息: “基础连接已关闭:接收时发生意外错误 问题Id:simplecms.Models.FtpConn.UploadFile中的System.Net.WebException”C# 将文件上载到FTP服务器在本地主机上工作,但在部署时不工作,c#,asp.net,azure,asp.net-core,ftp,C#,Asp.net,Azure,Asp.net Core,Ftp,我花了一整天的时间想弄明白这一点,但运气不好 基本上,我有一堆文件和文件夹,我想上传到FTP服务器上,所有的东西都可以在本地主机上运行。但当我将它部署到azure时,它就停止工作了。在我看来,这毫无意义,我最初的想法是它与防火墙、上传超时或其他东西有关 您是否看到我的代码中缺少什么,或者我是否需要在azure或ftp服务器上进行一些配置 编辑:我忘记包含错误消息: “基础连接已关闭:接收时发生意外错误 问题Id:simplecms.Models.FtpConn.UploadFile中的Syste
如果你能把异常记录到某个地方,你可能会有更好的主意,谢谢!我完全忘了在我的问题中包括这一点。在azure上,我实际上收到了错误消息。我将编辑我的问题。那是什么“azure”?是满的吗?或者只是一个“网站”?+可以发布吗?了解实际的web异常可能有助于诊断您的问题。。。你能得到吗?也许还包括堆栈跟踪?
public class FtpConn
{
public string UploadCms(string address, string username, string password, string host, bool userftp, string guid)
{
CreateFileUID(guid);
string location;
if (userftp)
{
location = address + "/simplecms/";
host = host + "/simplecms/";
}
else
{
location = address + "/simplecms" + guid + "/";
host = host + "/simplecms" + guid + "/";
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location);
request.Credentials = new NetworkCredential(username, password);
//request.Timeout = -1;
request.UsePassive = true;
request.UseBinary = true;
request.Timeout = 1000000000;
request.KeepAlive = false;
request.ReadWriteTimeout = 1000000000;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
string cmsFolder = "wwwroot/dist/";
string[] cmsFiles = Directory.GetFiles(cmsFolder);
foreach (string file in cmsFiles)
{
string path = Path.GetFullPath(file);
string name = Path.GetFileName(file);
UploadFile(username, password, location, path, name);
}
string[] staticFiles = Directory.GetDirectories(cmsFolder);
string[] subs = Directory.GetDirectories(cmsFolder + "static/");
foreach (string file in staticFiles)
{
CreateDirectory(username, password, location + "/" + Path.GetFileName(file));
}
foreach (string folder in subs)
{
CreateDirectory(username, password, location + "/static/" + Path.GetFileName(folder));
foreach (string subfile in Directory.GetFiles(folder))
{
string path = Path.GetFullPath(subfile);
string name = Path.GetFileName(subfile);
UploadFile(username, password, location + "/static/" + Path.GetFileName(folder) + "/" + Path.GetFileName(subfile), path, "");
}
}
return host;
}
private void UploadFile(string username, string password, string location, string filePath, string fileName)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location + fileName);
request.Credentials = new NetworkCredential(username, password);
//request.Timeout = -1;
request.UsePassive = true;
request.UseBinary = true;
request.Timeout = 1000000000;
request.KeepAlive = false;
request.ReadWriteTimeout = 1000000000;
request.Method = WebRequestMethods.Ftp.UploadFile;
FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
}
private void CreateDirectory(string username, string password, string newDirectory)
{
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(newDirectory);
ftpRequest.Credentials = new NetworkCredential(username, password);
//ftpRequest.Timeout = -1;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex); }
return;
}
private void CreateFileUID(string uid)
{
string folderName = "wwwroot/dist/";
string[] txtList = Directory.GetFiles(folderName, "*.txt");
foreach (string f in txtList)
{
File.Delete(f);
}
string fileName = "uid.txt";
string pathString = Path.Combine(folderName, fileName);
using (FileStream fs = File.Create(pathString))
{
byte[] SimpleCmsUID = new UTF8Encoding(true).GetBytes(uid);
fs.Write(SimpleCmsUID, 0, SimpleCmsUID.Length);
}
}
}