12

ファイルをドラッグして自動的に ftp にアップロードする単純な Windows アプリケーションを作成しています。

ここに画像の説明を入力

MSDNコードを使用してファイルをFTPにアップロードしています。

コードは非常に簡単です。

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format("{0}{1}", FTP_PATH, filenameToUpload));
request.Method = WebRequestMethods.Ftp.UploadFile;

// Options
request.UseBinary = true;
request.UsePassive = false;

// FTP Credentials
request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);

// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fileToUpload.FullName);
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();
writeOutput("Upload File Complete!");
writeOutput("Status: " + response.StatusDescription);

response.Close();

FTPにアップロードされます

ここに画像の説明を入力

問題は、ブラウザでファイルを表示するとき、または単にダウンロードしてデスクトップで表示しようとすると、次のようになることです。

ここに画像の説明を入力

私はすでに使用request.UseBinary = false;request.UsePassive = false;ていますが、何の役にも立ちません。

私が見つけたのは、元のファイルの長さが122Kbで、FTP(およびダウンロード後)では219Kbであるということです...

私は何を間違っていますか?

ちなみに、uploadFileToFTP()メソッドは 内で実行されていBackgroundWorkerますが、実際には違いはありません...

4

2 に答える 2

32

バイナリ ファイルを読み取るには、StreamReader を使用するのではなく、Stream のみを使用する必要があります。

Streamreader は、テキスト ファイルのみを読み取るように設計されています。

これを試してください:

private static void up(string sourceFile, string targetFile)
{            
    try
    {
        string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"];
        string ftpUserID = ConfigurationManager.AppSettings["ftpUser"];
        string ftpPassword = ConfigurationManager.AppSettings["ftpPass"];
        ////string ftpURI = "";
        string filename = "ftp://" + ftpServerIP + "//" + targetFile; 
        FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
        ftpReq.UseBinary = true;
        ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
        ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

        byte[] b = File.ReadAllBytes(sourceFile);

        ftpReq.ContentLength = b.Length;
        using (Stream s = ftpReq.GetRequestStream())
        {
            s.Write(b, 0, b.Length);
        }

        FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

        if (ftpResp != null)
        {
            MessageBox.Show(ftpResp.StatusDescription);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
于 2012-04-26T09:34:56.263 に答える
3

この問題は、バイナリ データを文字データにデコードし、バイナリ データに戻すコードが原因で発生します。これをしないでください。


WebClient クラスのUploadFile メソッドを使用します。

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);
    client.UploadFile(FTP_PATH + filenameToUpload, filenameToUpload);
}
于 2012-04-26T09:34:48.423 に答える