2

我有一个允许会员上传照片的网站。在 MVC 控制器中,我将 'FormCollection作为 Action 的参数。然后我将第一个文件读取为 type HttpPostedFileBase。我用它来生成缩略图。这一切都很好。

除了允许会员上传自己的照片外,我还想自己使用System.Net.WebClient导入照片。

我试图概括处理上传照片(文件)的方法,以便它可以采用通用 Stream 对象而不是特定的HttpPostedFileBase.

我试图基于 Stream 的所有内容,因为它HttpPostedFileBase有一个InputStream包含文件流的属性,并且WebClient有一个OpenRead返回 Stream 的方法。

但是,通过使用 Stream over HttpPostedFileBase,看起来我正在丢失用于验证文件的属性ContentTypeContentLength

以前没有使用过二进制流,有没有办法从流中获取ContentTypeContentLength?或者有没有办法HttpPostedFileBase使用 Stream 创建一个对象?

4

1 回答 1

3

您从原始流的角度来看它是正确的,因为这样您就可以创建一种方法来处理流,从而处理它们来自的许多场景。

在文件上传场景中,您获取的流位于与内容类型不同的属性上。有时可以使用幻数这里也是一个很好的来源)通过流标头字节来检测数据类型,但这可能有点过头了,因为您已经可以通过其他方式(即 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));

为具有您可以解析和识别的内容标识符的任何流调用相同的自定义方法。

于 2010-11-14T06:10:15.623 回答