asp.net-mvc - System.IO.Stream 支持 HttpPostedFileBase

标签 asp.net-mvc webclient httppostedfilebase

我有一个允许成员(member)上传照片的网站。在 MVC Controller 中,我使用 FormCollection作为 Action 的参数。然后我将第一个文件读取为类型 HttpPostedFileBase .我用它来生成缩略图。这一切正常。

除了允许成员(member)上传自己的照片,我想使用System.Net.WebClient自己导入照片。

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

HttpPostedFileBase 以来,我试图将所有内容都基于 Stream有一个 InputStream包含文件流和 WebClient 的属性有一个 OpenRead返回 Stream 的方法。

但是,通过使用 Stream 超过 HttpPostedFileBase ,看来我要输了ContentTypeContentLength我用于验证文件的属性。

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

最佳答案

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

在文件上传场景中,您获取的流位于与内容类型不同的属性上。有时 magic numbers ( also a great source here ) 可用于通过流 header 字节检测数据类型,但这可能有点矫枉过正,因为您已经可以通过其他方式(即 Content-Type header 或 .ext 文件扩展名等)获得数据)。

您可以通过读取流来测量流的字节长度,因此您实际上并不需要 Content-Length header :浏览器只是发现提前知道预期的文件大小很有用。

如果您的 网络客户端 正在访问 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);

使用 时网络客户端 实例名为 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));

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

关于asp.net-mvc - System.IO.Stream 支持 HttpPostedFileBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4176349/

相关文章:

asp.net-mvc - 使用 ASP.NET MVC 和表单例份验证的不同 URL 的不同登录 URL

asp.net-mvc - 如何以特定域用户身份运行 Internet Explorer Selenium 测试?

c# - ASP.NET MVC - 在 OnActionExecuted 中修改 View 结果

c# - WebClient 下载字符串(几个字符页面)太慢了

java - 如何覆盖任何 HttpHeader 以响应 WebClient?

c# - 对于基本 View 模型属性,HttpPostedFileBase 在 POST 上为 0 字节

jquery - 单击后仍保持CSS样式

c# - WebClient.DownloadFile的莫名其妙的行为

c# - ASP.NET MVC 4,C# : how to Download File

asp.net-mvc - 模拟 HttpPostedFileBase 和 InputStream 进行单元测试