0

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

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

该表包含三个信息,我应该使用它们:

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

application/x-zip-compressed application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document application/pdf application/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);

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

非常感谢。

4

1 回答 1

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();
于 2015-10-27T22:20:36.593 回答