您从原始流的角度来看它是正确的,因为这样您就可以创建一种方法来处理流,从而处理它们来自的许多场景。
在文件上传场景中,您获取的流位于与内容类型不同的属性上。有时可以使用幻数(这里也是一个很好的来源)通过流标头字节来检测数据类型,但这可能有点过头了,因为您已经可以通过其他方式(即 Content-Type 标头或 . ext 文件扩展名等)。
您可以仅通过读取流来测量流的字节长度,因此您实际上并不需要 Content-Length 标头:浏览器只是发现提前知道预期的文件大小很有用。
如果您的WebClient正在访问 Internet 上的资源 URI,它将知道像http://www.example.com/image这样的文件扩展名。gif,这可能是一个很好的文件类型标识符。
由于您已经可以使用文件信息,为什么不在您的自定义处理方法上再打开一个参数来接受内容类型字符串标识符,例如:
public static class Custom {
// Works with a stream from any source and a content type string indentifier.
static public void SavePicture(Stream inStream, string contentIdentifer) {
// Parse and recognize contentIdentifer to know the kind of file.
// Read the bytes of the file in the stream (while counting them).
// Write the bytes to wherever the destination is (e.g. disk)
// Example:
long totalBytesSeen = 0L;
byte[] bytes = new byte[1024]; //1K buffer to store bytes.
// Read one chunk of bytes at a time.
do
{
int num = inStream.Read(bytes, 0, 1024); // read up to 1024 bytes
// No bytes read means end of file.
if (num == 0)
break; // good bye
totalBytesSeen += num; //Actual length is accumulating.
/* Can check for "magic number" here, while reading this stream
* in the case the file extension or content-type cannot be trusted.
*/
/* Write logic here to write the byte buffer to
* disk or do what you want with them.
*/
} while (true);
}
}
IO 命名空间中有一些有用的文件名解析功能:
using System.IO;
在您提到的场景中使用您的自定义方法,如下所示:
从名为的HttpPostedFileBase实例myPostedFile
Custom.SavePicture(myPostedFile.InputStream, myPostedFile.ContentType);
使用名为的WebClient实例时webClient1
:
var imageFilename = "pic.gif";
var stream = webClient1.DownloadFile("http://www.example.com/images/", imageFilename)
//...
Custom.SavePicture(stream, Path.GetExtension(imageFilename));
甚至在从磁盘处理文件时:
Custom.SavePicture(File.Open(pathToFile), Path.GetExtension(pathToFile));
为具有您可以解析和识别的内容标识符的任何流调用相同的自定义方法。