Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么MemoryStream.Length(或byte[].Length)与C#中的HttpPostedFile.ContentLength不同?_C#_Asp.net_Image_Stream_Memorystream - Fatal编程技术网

为什么MemoryStream.Length(或byte[].Length)与C#中的HttpPostedFile.ContentLength不同?

为什么MemoryStream.Length(或byte[].Length)与C#中的HttpPostedFile.ContentLength不同?,c#,asp.net,image,stream,memorystream,C#,Asp.net,Image,Stream,Memorystream,我有一个base64编码的图像需要发送到某个服务器。我所做的: string content = (string) someDynamicObjectFromJson; byte[] imageBytes = Convert.FromBase64String(content); var actualImage = new MemoryStream(imageBytes, 0, imageBytes.Length); long contentLength = imageBytes.Length;

我有一个base64编码的图像需要发送到某个服务器。我所做的:

string content = (string) someDynamicObjectFromJson;
byte[] imageBytes = Convert.FromBase64String(content); 
var actualImage = new MemoryStream(imageBytes, 0, imageBytes.Length);
long contentLength = imageBytes.Length; // or actualImage.Length
对于某些图像,我得到的
内容长度
等于
25

但是当我从html表单上传相同的图像并使用
HttpPostedFileBase
param定义控制器操作时,我得到
content length
等于'53219':

public virtual ActionResult UploadPartyImages(HttpPostedFileBase image)
{
    long contentLength = image.ContentLength; // equals to '34451'
}
HttpPostedFileBase
中,我可以将这个
contentLength
分配给
HttpWebRequest.contentLength

request.ContentLength = contentLength;
并将图像上传到服务器。在分配MemoryStream.Length的情况下,我得到:

无法从传输连接读取数据:已建立 主机中的软件中止了连接

MSDN的内容:

1) HttpPostedFile.ContentLength—获取流的长度(以字节为单位)。
2) Length-获取流的长度(以字节为单位)

问题:

1) 如果描述相同,为什么我得到的值如此不同(25和53219)?
2) 从MemoryStream(或者更确切地说是base64 image)获取
ContentLength
的正确方法是什么,这样我就可以设置内容长度http头了

编辑:

我读到可以保存文件,然后创建
FileInfo
实例。和调用长度属性。但是我认为这不是处理这些事情的正确方法。

base64字符串不像表单上载那样包含相同的图像/字节。另外,25字节对于图像来说非常小。您不应该分配HttpWebRequest.ContentLength,因为内容长度不是图像的长度。它是HTTP协议的表单编码字节的长度。使用GetRequestStream。事实上,它甚至比这更复杂。使用Fiddler查看带有文件输入的格式正确的帖子的外观。