如何从请求流写入.csv
本文关键字:csv 请求 | 更新日期: 2023-09-27 18:01:21
我正在尝试从我的网站上传一个。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);