如何从请求流中写入 .csv

How to write .csv from request stream

我正在尝试将 .csv 文件从我的网站上传到我的服务器。

文件被很好地复制到服务器,并且与最初上传的 .csv 文件大小相同。

问题是当我在服务器上打开 .csv 文件时,文件的内容是空的。

这是我的上传方法:

public bool UploadCsvFile(Stream uploadFileStream, string fileExtension)
        {
            byte[] fileData = new byte[(int)uploadFileStream.Length];
            string hashname = BitConverter.ToString(MD5CryptoServiceProvider.Create().ComputeHash(fileData)).Replace("-", string.Empty);
            DateTime now = DateTime.Now;
            string originalImageName = hashname + now.Year + now.Day + now.Month + now.Hour + now.Minute + now.Second + now.Millisecond + fileExtension;
            var dropFolder = ConfigurationManager.AppSettings["mydrop"];

            var fileNamePath = Path.Combine(dropFolder, originalImageName);

            try
             {
              try
               {
                   var fileStream = File.Create(fileNamePath);
                   fileStream.Write(fileData, 0, fileData.Length);
                  fileStream.Close();

            //I also tried this:
                  //var fileStream = File.Create(fileNamePath, (int)uploadFileStream.Length);
                 // fileStream.Read(fileData, 0, fileData.Length);
                  //fileStream.Write(fileData, 0, fileData.Length);
                 // fileStream.Close();


                }
                catch (Exception exc)
                {
                    throw exc;
                }
              }
                catch (Exception ex)
                {
                    throw ex;
                }

            return true;
        }

您应该能够像这样简单地完成这项工作:

var fileStream = File.Create(fileNamePath);

uploadFileStream.copyTo(fileStream);