Download file directly to memory

依然范特西╮ 提交于 2019-11-27 13:56:51

问题


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) method. My issue is I'm not sure if it's possible to download a file directly into memory. Anyone know if this is possible?


回答1:


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);
}



回答2:


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;
    }


来源:https://stackoverflow.com/questions/19686599/download-file-directly-to-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!