Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file directly to memory

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?

like image 558
daved Avatar asked Oct 30 '13 15:10

daved


People also ask

How do you create a memory file in Python?

Python supports file like objects, that don't write to the disk but stay in the memory. You can create file like objects with StringIO. From Python version > 3 this is part of the io module. These files live only inside the computer memory, not on the disk.


2 Answers

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); } 
like image 97
Attila Avatar answered Oct 02 '22 15:10

Attila


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.

like image 26
Grant Winney Avatar answered Oct 02 '22 16:10

Grant Winney