如何将二进制文件数据转换为具有给定MIME类型的物理类型

时间:2015-10-27 18:38:57

标签: c# c#-4.0 system.io.file system.io.fileinfo

我有一个数据库表(tbl_document),其中包含随时间上传到其中的文件的详细信息。

我想编写一个控制台应用程序来将这些文件下载到指定位置。

该表有三条信息,我应该使用:

varbinary格式的 FileContents ; 每个文件的 MIME类型 contentType ),采用nvarchar格式,例如:

应用程序/ x-ZIP压缩的 应用程序/ msword 应用/ vnd.openxmlformats-officedocument.wordprocessingml.document 应用程序/ PDF格式 应用/ PDF

在MVC中,我会做这样的事情来执行这项任务:

public FileContentResult DownloadFile()
{
                FileContentResult result = new FileContentResult(file.FileContents, file.ContentType);
                result.FileDownloadName = file.FileName;
                return result;
}

我尝试过其他方法来做这个:

WebClient myWebClient = new WebClient();
 FileContentResult result = new FileContentResult(fileContents, contentType);

我无法使用上述任何方法保存文件。请你帮忙。

非常感谢。

1 个答案:

答案 0 :(得分:1)

我实际上已经解决了这个问题:

 // fileContents is the binary file being downloaded; I didn't need to use the MIME Types
        MemoryStream ms = new MemoryStream(fileContents);
        //write to file
        FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write);
        ms.WriteTo(file);
        file.Close();
        ms.Close();