将base64String保存为FTP服务器上的映像,将保存损坏的文件

将base64String保存为FTP服务器上的映像,将保存损坏的文件,ftp,ftp-client,Ftp,Ftp Client,将base64String保存为FTP服务器上的映像会将其保存为损坏的文件。 我在做以下事情 已将base64String转换为字节[] 已初始化MemoryStream,并在上述步骤中转换了字节 从FTP打开的流 在ftp上写入流 下面是代码 public bool WriteFromBase64ToFile(string base64, string path, string fileName) { bool result = false; usin

将base64String保存为FTP服务器上的映像会将其保存为损坏的文件。 我在做以下事情

  • 已将base64String转换为字节[]
  • 已初始化MemoryStream,并在上述步骤中转换了字节
  • 从FTP打开的流
  • 在ftp上写入流
  • 下面是代码

    public bool WriteFromBase64ToFile(string base64, string path, string fileName)
        {
            bool result = false;
    
            using (FtpClient ftp = new FtpClient())
            {
    
                // setting ftp properties with required values.
                ftp.ReadTimeout = 999999999;
                ftp.Host = host;
                ftp.Credentials = new System.Net.NetworkCredential(username, password);
                ftp.Port = Convert.ToInt32(port);
                ftp.DataConnectionType = FtpDataConnectionType.AutoPassive;
                ftp.Connect();
                ftp.ConnectTimeout = 1000000;
    
                // converting base64String into byte array.
                byte[] file = Convert.FromBase64String(base64);
    
    
    
                if (ftp.IsConnected)
                {
                    int BUFFER_SIZE = file.Length; // 64KB buffer
                    byte[] buffer = new byte[file.Length];
    
                    // Initializing MemoryStream with byte converted from base64String.
                    MemoryStream ms = new MemoryStream(buffer);
    
                    using (Stream readStream = ms)
                    {
                        fileName = fileName.ReplacingSpecialCharacterswithEntities();
    
                        // Getting stream from ftp and then writing it on FTP server.
                        using (Stream writeStream = ftp.OpenWrite(path + "/" + fileName+".jpg", FtpDataType.Binary))
                        {
    
                            while (readStream.Position < readStream.Length)
                            {
                                buffer.Initialize();
    
                                // Reading stream
                                int bytesRead = readStream.Read(buffer, 0, BUFFER_SIZE);
                                // Writing stream
                                writeStream.Write(buffer, 0, bytesRead);
                            }
    
                            // flushing stream.
                            writeStream.Flush();
                        }
                    }
                }
            }
            result = true;
            return result;
        }
    
    public bool WriteFromBase64ToFile(字符串base64、字符串路径、字符串文件名)
    {
    布尔结果=假;
    使用(FtpClient ftp=new FtpClient())
    {
    //使用所需值设置ftp属性。
    ftp.ReadTimeout=9999999;
    ftp.Host=Host;
    ftp.Credentials=新系统.Net.NetworkCredential(用户名、密码);
    ftp.Port=Convert.ToInt32(端口);
    ftp.DataConnectionType=FtpDataConnectionType.AutoPassive;
    ftp.Connect();
    ftp.ConnectTimeout=1000000;
    //将base64String转换为字节数组。
    byte[]file=Convert.FromBase64String(base64);
    如果(ftp.IsConnected)
    {
    int BUFFER_SIZE=file.Length;//64KB缓冲区
    byte[]buffer=新字节[file.Length];
    //使用从base64String转换的字节初始化MemoryStream。
    MemoryStream ms=新的MemoryStream(缓冲区);
    使用(Stream readStream=ms)
    {
    fileName=fileName.ReplacingSpecialCharacterswithenties();
    //从ftp获取流,然后将其写入ftp服务器。
    使用(Stream writeStream=ftp.OpenWrite(path+“/”+fileName+”.jpg),FtpDataType.Binary))
    {
    while(readStream.Position
    它是如何损坏的?每个字节都错了?完全不同的长度?还是只有几个字节?尝试设置二进制模式,而不是ASCII。实际上已将所有字节保存为二进制,但当我打开文件时,它会显示“Windows Photo Viewer无法打开此图片,因为文件已损坏或太大。”Windows picture Viewer是否理解base64字符串?标记,若你们看过我的代码,我正在字节数组中转换base64String,然后在流中转换字节数组,然后在FTP上写入流。我认为这就是我们如何将base64String数据保存在二进制文件中的过程,无论它是图像文件还是其他格式文件。