使用WebRequest上载到FTP服务器后,文件已损坏

本文关键字:文件 已损坏 服务器 FTP WebRequest 上载 使用 | 更新日期: 2023-09-27 18:23:44

将.csv文件传输到FTP服务器时出现了一些问题。在转移之前,它是可以的,但当我在FTP上检查它时,它看起来像坏了什么的:

"ЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  T8пїЅпїЅпїЅпїЅпїЅпїЅ'p3>@L @E8?5=:>                                                                               BпїЅaпїЅ="

编码有问题吗?我正在使用这种下载方法:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxxxxxxx.xx/" + name);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.KeepAlive = true;
        request.UseBinary = true;
        request.Credentials = new NetworkCredential("xxxx", "qweqwe123");
        StreamReader sourceStream = new StreamReader("F:''" + xxxx);
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        response.Close();

使用WebRequest上载到FTP服务器后,文件已损坏

不喜欢回答我的问题,但太长了,无法发表评论:解决了,也许有人有这个问题。尝试使用这种上传方法:

FileInfo toUpload = new FileInfo("log.txt");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://csharpcoderr.com/public_html/" + toUpload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("name", "pass");
Stream ftpStream = request.GetRequestStream();
FileStream fileStream = File.OpenRead("log.txt");
byte[] buffer = new byte[1024];
int bytesRead = 0;
do
{
   bytesRead = fileStream.Read(buffer, 0, 1024);
   ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
fileStream.Close();
ftpStream.Close();

我可以用同样的问题来解决这个问题。正在扩展Brawl_Ups解决方案。。。而且似乎最好将BinaryReader用于二进制文件。这是我偶然想到的一个片段。。。这是目前正在进行的工作,欢迎任何编辑。

public string UploadFile()
{
    string sRetVal = string.Empty;
    string sFullDestination = this.DestinatinFullIDPath + this.UpLoadFileName;
    try
    {
        if ((this.CreateDestinationDir(this.DestinatinFullModulePath) == true) &
            (this.CreateDestinationDir(this.DestinatinFullIDPath) == true))
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(sFullDestination);
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.Credentials = new NetworkCredential(this.UserName, this.Password);
            ftpRequest.UsePassive = true;
            ftpRequest.UseBinary = true;
            ftpRequest.EnableSsl = false;
            byte[] fileContents;
            using (BinaryReader binReader = new BinaryReader(File.OpenRead(this.UpLoadFullName)))
            {
                FileInfo fi = new FileInfo(this.UpLoadFullName);
                binReader.BaseStream.Position = 0;
                fileContents = binReader.ReadBytes((int)fi.Length);
            }
            ftpRequest.ContentLength = fileContents.Length;
            using (Stream requestStream = ftpRequest.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }
            using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse())
            {
                sRetVal = string.Format("Upload File Complete, status {0}", response.StatusDescription);
            }
        }
    }
    catch (WebException ex)
    {
        throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
    }
    return sRetVal;
}