我创建了一个WCF Rest服务,我正在阅读multipart / form数据发送的内容。但是,我从流中获取内容配置和额外信息。 导致文件损坏,特别是图像。我的服务托管在IIS上。以下是用于实现的代码。当我上传文件时,文件会添加额外的内容。如何删除额外内容?
public string UploadFile(Stream fileContent)
{
try
{
FileStream targetStream = null;
string uploadFolder = @"C:\Upload";
string filePath = Path.Combine(uploadFolder, "123");
using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 45000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = fileContent.Read(buffer, 0, bufferLen)) > 0)
{
// save to output stream
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
targetStream.Dispose();
}
}
catch (Exception ex)
{
return ex.Message;
}
return "Image Uploaded";
}