将文件直接下载到内存

我想直接从ftp站点将excel文件加载到内存流中。 然后我想使用OpenExcel(Stream)方法在FarPoint Spread控件中打开该文件。 我的问题是我不确定是否可以将文件直接下载到内存中。 有人知道这是否可行?

是的,您可以将文件从FTP下载到内存。

我想你甚至可以从FTP服务器传递Stream来由FarPoint处理。

WebRequest request = FtpWebRequest.Create("ftp://asd.com/file"); using (WebResponse response = request.GetResponse()) { Stream responseStream = response.GetResponseStream(); OpenExcel(responseStream); } 

使用WebClient几乎可以做到。 通常使用WebClient更容易,但为您提供更少的配置选项和控制(例如:无超时设置)。

 WebClient wc = new WebClient(); using (MemoryStream stream = new MemoryStream(wc.DownloadData("ftp://asd.com/file"))) { OpenExcel(stream); } 

看看WebClient.DownloadData 。 您应该能够将文件目录下载到内存中,而不是先将其写入文件。

这是未经测试的,但类似于:

 var spreadSheetStream = new MemoryStream(new WebClient().DownloadData(yourFilePath)); 

我不熟悉FarPoint,说明流是否可以直接与OpenExcel方法一起使用。 在线示例显示了与FileStream一起使用的方法,但我假设可以接受任何类型的Stream

将文件从URL下载到内存。 我的答案并没有完全显示,如何下载文件以便在Excel中使用,但展示了如何创建通用的内存中字节数组。

  private static byte[] DownloadFile(string url) { byte[] result = null; using (WebClient webClient = new WebClient()) { result = webClient.DownloadData(url); } return result; }