簡體   English   中英

通過ftp上傳文本文檔,但文件為空白/空

[英]Upload a text document by ftp but the file is blank / empty

以下是將txt文件從一個位置復制到ftp路徑的代碼段:

WebRequest WRequest = WebRequest.Create(FtpPath + OriginalfileName);
WRequest.Method = WebRequestMethods.Ftp.UploadFile;
WRequest.Credentials = new NetworkCredential("myusername", "FtpPassword");
FileStream stream = File.OpenRead(OriginalFilePath);
byte[] buffer = new byte[stream.Length];
Stream RStream = WRequest.GetRequestStream();
RStream.Write(buffer, 0, buffer.Length);
RStream.Close();

但是在ftp目標復制的文件始終為空。 為什么?

您沒有用文件的內容填充緩沖區。 您只需在以下代碼行中設置長度:

byte[] buffer = new byte[stream.Length];

而且,您也不會在其他任何地方填充文件的內容,因此您只是向FTP服務器發送空數據。 字節可能有一定的長度,但是全都是空字節。

作為一個簡單的解決方案,您可以使用WebClient

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(uriString,fileName);

您可以在寫入字節時應用相同類型的邏輯,但在我輸入初始大小的情況下除外

public static void CopyStream(Stream input, Stream output)
{

    byte[] buffer = new byte[1024];

    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}

這應該工作:

WebRequest WRequest = WebRequest.Create(FtpPath + OriginalfileName);
WRequest.Method = WebRequestMethods.Ftp.UploadFile;
WRequest.Credentials = new NetworkCredential("myusername", "FtpPassword");
using (FileStream stream = File.OpenRead(OriginalFilePath)) {
  using (Stream RStream = WRequest.GetRequestStream()) {
    stream.CopyTo(RStream);
  }
}

這是在asp.net core 2.1中使用IFormFile上傳文件的解決方案

// Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("*******/site/wwwroot/wwwroot/images/" + TempFileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.UseBinary = true;
                request.KeepAlive = false;
                request.Method = "STOR";
                request.Credentials = new NetworkCredential("***", "****");

                // Copy the contents of the file to the request stream.
                byte[] fileContents = null;

                if (vehicle.FileUpload.Length > 0)
                {
                    using (var ms = new MemoryStream())
                    {
                        await vehicle.FileUpload.CopyToAsync(ms);
                        var fileBytes = ms.ToArray();
                        fileContents = fileBytes;
                    }
                }
                request.ContentLength = fileContents.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(fileContents, 0, fileContents.Length);
                }

這是文件的視圖模型

public class VehicleSeriesViewModel
    {
        public Guid VehicleBadgeId { get; set; }
        public Guid VehicleSeriesId { get; set; }
        public string Name { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime? UpdatedAt { get; set; }
        public IFormFile FileUpload { get; set; }
        public string PictureUrl { get; set; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM