简体   繁体   English

使用WebRequest上传到FTP服务器后文件损坏

[英]File corrupted after upload to FTP server with WebRequest

Here's some problem with transporting .csv file to FTP server. 这是将.csv文件传输到FTP服务器的一些问题。 Before transfering it's okay, but when i'm checking it on FTP it looks like broken or something: 传输之前是可以的,但是当我在FTP上检查它时,它看起来好像坏了或有些东西:

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

Is it problem with encoding? 编码有问题吗? I'm using this method of download: 我正在使用这种下载方法:

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();

Don't like to answer under my aquestions, but it's too long for comment: Solved, maybe somebody have this problem. 我不愿意回答我的问题,但是评论太久了:解决了,也许有人遇到了这个问题。 Try to use this upload method: 尝试使用此上传方法:

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();

I can across this question with the same issue. 我可以通过相同的问题来解决这个问题。 Expanding on Brawl_Ups solution... And it seems that its best to use the BinaryReader for binary files. 扩展Brawl_Ups解决方案...似乎最好对二进制文件使用BinaryReader。 This is a snippet I eventfully came up with... This is a current work in progress and any edits are welcome. 这是我最终想出的摘要...这是一项正在进行的工作,欢迎您进行任何编辑。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM