Download file directly to memory

前端 未结 3 1510
你的背包
你的背包 2020-12-14 17:35

I would like to load an excel file directly from an ftp site into a memory stream. Then I want to open the file in the FarPoint Spread control using the OpenExcel(Stream) me

相关标签:
3条回答
  • 2020-12-14 17:59

    Yes, you can download a file from FTP to memory.

    I think you can even pass the Stream from the FTP server to be processed by FarPoint.

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

    Using WebClient you can do nearly the same. Generally using WebClient is easier but gives you less configuration options and control (eg.: No timeout setting).

    WebClient wc = new WebClient();
    using (MemoryStream stream = new MemoryStream(wc.DownloadData("ftp://asd.com/file")))
    {
        OpenExcel(stream);
    }
    
    0 讨论(0)
  • 2020-12-14 18:23

    Take a look at WebClient.DownloadData. You should be able to download the file directory to memory and not write it to a file first.

    This is untested, but something like:

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

    I'm not familiar with FarPoint though, to say whether or not the stream can be used directly with the OpenExcel method. Online examples show the method being used with a FileStream, but I'd assume any kind of Stream would be accepted.

    0 讨论(0)
  • 2020-12-14 18:24

    Download file from URL to memory. My answer does not exactly show, how to download a file for use in Excel, but shows how to create a generic-purpose in-memory byte array.

        private static byte[] DownloadFile(string url)
        {
            byte[] result = null;
    
            using (WebClient webClient = new WebClient())
            {
                result = webClient.DownloadData(url);
            }
    
            return result;
        }
    
    0 讨论(0)
提交回复
热议问题