简体   繁体   English

C#-文件上传到服务器后损坏

[英]C# - File is corrupt after uploaded to server

I used the following source code for upload file excel and pdf, but after the file was moved to server, the file is corrupt. 我使用以下源代码上传excel和pdf文件,但是将文件移至服务器后,文件已损坏。 I think the problem is on encoding process Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 我认为问题在于编码过程Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); , but I don't know how to resolve it. ,但我不知道如何解决。

public static void sampleUpload()
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://100.165.80.15:21/output/Group Dealer, Main Dealer, Zone, Branch, and Destination Report_20120927105003.pdf");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("toc", "fid123!!");

        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader("D:\\Group Dealer, Main Dealer, Zone, Branch, and Destination Report_20120927105003.pdf");
        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();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
    }

Don't read binary files as text. 不要将二进制文件读取为文本。 Use Stream.CopyTo method (or equivalent code if you can't use .Net 4.0) 使用Stream.CopyTo方法(或等效代码,如果您不能使用.Net 4.0)

 using(StreamReader sourceStream = ...){
   using(Stream requestStream = request.GetRequestStream())
   {
     sourceStream.CopyTo(requestStream);
   }
 }

您可以尝试使用处理原始字节的BufferedStream。

In my situation I couldn't use Stream.Copy as suggested by Alexei in his answer because I was using .NET Framework 2.0 I instead used only a Stream to read binary files as Streamreader is designed to read text files only: 在我的情况下,我无法使用Alexei在他的回答中建议的Stream.Copy,因为我使用的是.NET Framework 2.0,而我只使用Stream来读取二进制文件,因为Streamreader设计为仅读取文本文件:

public static void sampleUpload()
{
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://100.165.80.15:21/output/Group Dealer, Main Dealer, Zone, Branch, and Destination Report_20120927105003.pdf");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UseBinary = true;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential("toc", "fid123!!");

    // Copy the contents of the file to the request stream.
    byte[] b = File.ReadAllBytes(sourceFile);

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

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

    response.Close();
}

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

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