29

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?

daved
  • 392
  • 1
  • 4
  • 11
  • 1
    Yes; just use streams. – SLaks Oct 30 '13 at 15:14
  • Okay, so I can load the file from ftp web address link directly into a stream instance? Or would I need to download the file to the user's machine first? I'd prefer the former if it's possible. – daved Oct 30 '13 at 15:19

3 Answers3

43

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);
}
Undo
  • 25,519
  • 37
  • 106
  • 129
Attila
  • 3,206
  • 2
  • 31
  • 44
  • Are we certain `WebResponse.GetResponseStream` doesn't shove the data into a temp file and return a `FileStream` or something similar? – Michael J. Gray Oct 30 '13 at 15:33
  • It doesn't even start downloading anything until you start reading the stream. I am sure about this, since if you don't continue reading for a while the servers going to timeout. see: http://msdn.microsoft.com/en-us/library/system.net.webresponse.getresponsestream(v=vs.110).aspx – Attila Oct 30 '13 at 15:40
25

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.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 1
    Found this useful for me. I was trying to download a .zip file from a website but was using new WebClient().OpenRead(file) to get the stream but getting a "stream does not support seek operation" errors. Dunno why this one worked but I appreciate it :) – tehAon Dec 15 '16 at 15:35
  • I used it and it works well I'm saving file to memory and download it for Client in browser ----------------------------------------------------------------------------------------------------------------------------------------------------------------- var pdf = new MemoryStream(new WebClient().DownloadData($"http://localhost:56110/Upload/{item.PDF}")).ToArray(); string fileName = item.Title + Path.GetExtension(item.PDF); return File(pdf, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName); – Abdelrhman Elsaid Oct 20 '20 at 12:15
1

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;
    }
Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69
  • Where is it? I can't see it! I rewrite this code to use in my project. So somene will need it. – Erçin Dedeoğlu May 03 '18 at 11:49
  • they are not complete&ready to use code snippets for begginer levels. – Erçin Dedeoğlu May 03 '18 at 12:39
  • 2
    Well, they do answer the question. What your answer actually does not. Maybe you should add some explanation text, like *"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"*. Though in general, one should prefer using streams instead of arrays, as in most cases, you do not all data in memory at the same time. – Martin Prikryl May 03 '18 at 12:54