简体   繁体   English

C#-字节数组和文件扩展名

[英]C# - Byte arrays and file extensions

I have a byte array that I've downloaded which lacks a file extension. 我下载的字节数组缺少文件扩展名。 I need to give this file an extension, which in my case will always be .gif, and convert back to a byte array. 我需要给该文件一个扩展名,在我的情况下,该扩展名将始终是.gif,然后转换回字节数组。

I'm accomplishing the first step thus: 我正在完成第一步:

FileContentResult file = File(imageList.Single(), "image/gif");

I'm not sure what to do next. 我不确定下一步该怎么做。 FileContentResult has a FileContents parameter which is of type byte array. FileContentResult具有一个FileContents参数,该参数的类型为字节数组。 I've tried using that, but it doesn't appear to contain the extension in the data. 我尝试使用它,但它似乎未在数据中包含扩展名。

Do byte arrays even contain the extension type? 字节数组甚至包含扩展类型吗? This is my first time working with one, so my understanding of what they are and what I can do with them is lacking. 这是我第一次与他们合作,因此缺乏对它们的含义以及我对它们可以做什么的理解。

image/gif is a mime type used on the internet to tell the client how to interpret a set of data. image / gif是一种在互联网上使用的哑剧类型,用于告诉客户端如何解释一组数据。

It is not required if you save the data to disk. 如果将数据保存到磁盘,则不需要。

I think maybe your wording of the problem domain is a little misleading. 我认为也许您对问题域的措辞有点误导。 When you use the term "downloaded", do you mean "retrieved by way of an http connection", or "read (past tense) from a disk file, or a database field"?? 当您使用“下载”一词时,是指“通过http连接检索”,还是“从磁盘文件或数据库字段读取(过去时)”?

I'm thinking it's the latter case -- so I'll proceed. 我认为是后一种情况-所以我继续。

Raw data, when pulled in this way, will NOT have a filename. 以这种方式提取的原始数据将没有文件名。 Why??? 为什么??? A filename is a pointer to some data within a file system. 文件名是文件系统中某些数据的指针。 If you have a brown paper bags marked "green things", and you happen to pull an apple out of the bag, there's intrinsically within that apple to associate that apple (raw data) with the bag marked "green things" (the pointer). 如果您有一个标有“绿色”的棕色纸袋,而您碰巧将一个苹果从包装袋中拉出,则该苹果内部本质上就是将那个苹果(原始数据)与标有“绿色”的袋子(指针)相关联。

Since your code makes reference to a FileContentResult, I infer that you are using MVC, and that you are within controller code, trying to deliver the byte array back to a browser. 由于您的代码引用了FileContentResult,因此我推断您正在使用MVC,并且您在控制器代码中,因此尝试将字节数组传递回浏览器。

If you take a peek at the link below here, you might find that the FileContentResult object has a neat property called "FileDownloadName". 如果您看一下下面的链接,可能会发现FileContentResult对象具有一个名为“ FileDownloadName”的简洁属性。 This is where you want to set the name of the file - append the extension. 这是您要设置文件名的位置-附加扩展名。

http://msdn.microsoft.com/en-us/library/system.web.mvc.filecontentresult(v=vs.118).aspx http://msdn.microsoft.com/en-us/library/system.web.mvc.filecontentresult(v=vs.118).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM