我有一个数据库表(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);
我无法使用上述任何方法保存文件。请你帮忙。
非常感谢。
答案 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();