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?
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);
}
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.
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;
}
Related
I have an ini config file located on Azure. I don't want to download this file which is how its currently being handled. I want to read it into a MemoryStream and parse it from there and then have the MemoryStream automatically flush the data.
Is there any way to do this without having to download the file itself onto the local drive?
Current download method is:
myWebClient.DownloadFile("AzureLink", #"C:\\Program Files (x86)\\MyProgram\\downloadedFile.ini")
I assume this is what you're looking for:
WebClient wc = new WebClient();
using (MemoryStream stream = new MemoryStream(wc.DownloadData(url)))
{
//your code in here
}
When using the following code to download a file:
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync("http://path/file, "localpath/file");
and an error occurs during the download (no internet connection, file not found, etc.)
it allocates a 0-byte file in localpath/file which can get quite annoying.
is there a way to avoid that in a clean way?
(i already just probe for 0 byte files on a download error and delete it, but i dont think that is the recommended solution)
If you reverse engineer the code for WebClient.DownloadFile you will see that the FileStream is instantiated before the download even begins. This is why the file will be created even if the download fails. There's no way to ammend that code so you should cosnider a different approach.
There are many ways to approach this problem. Consider using WebClient.DownloadData rather than WebClient.DownloadFile and only creating or writing to a file when the download is complete and you are sure you have the data you want.
WebClient client = new WebClient();
client.DownloadDataCompleted += (sender, eventArgs) =>
{
byte[] fileData = eventArgs.Result;
//did you receive the data successfully? Place your own condition here.
using (FileStream fileStream = new FileStream("C:\\Users\\Alex\\Desktop\\Data.rar", FileMode.Create))
fileStream.Write(fileData, 0, fileData.Length);
};
client.DownloadDataAsync(address);
client.Dispose();
I am downloading image files from web using the following code in my Console Application.
WebClient client = new WebClient();
client.DownloadFile(string address_of_image_file,string filename);
The code is running absolutely fine.
I want to know if there is a way i can get the size of this image file before I download it.
PS- Actually I have written code to make a crawler which moves around the site downloading image files. So I doesn't know its size beforehand. All I have is the complete path of file which has been extracted from the source of webpage.
Here is a simple example you can try
if you have files of different extensions like .GIF, .JPG, etc
you can create a variable or wrap the code within a Switch Case Statement
System.Net.WebClient client = new System.Net.WebClient();
client.OpenRead("http://someURL.com/Images/MyImage.jpg");
Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])
MessageBox.Show(bytes_total.ToString() + " Bytes");
If the web-service gives you a Content-Length HTTP header then it will be the image file size. However, if the web-service wants to "stream" data to you (using Chunk encoding), then you won't know until the whole file is downloaded.
You can use this code:
using System.Net;
public long GetFileSize(string url)
{
long result = 0;
WebRequest req = WebRequest.Create(url);
req.Method = "HEAD";
using (WebResponse resp = req.GetResponse())
{
if (long.TryParse(resp.Headers.Get("Content-Length"), out long contentLength))
{
result = contentLength;
}
}
return result;
}
You can use an HttpWebRequest to query the HEAD Method of the file and check the Content-Length in the response
You should look at this answer: C# Get http:/…/File Size where your question is fully explained. It's using HEAD HTTP request to retrieve the file size, but you can also read "Content-Length" header during GET request before reading response stream.
I am in a situation where I have to download one file into Bytearray and make some changes in that byte array, also at the same time I have to download another large file and merge the first file into it with the modified bytes on the fly and play it using MediaElement.
Using Webclient I am able to download file but the webclient only gives me access to its dwonloaded bytearray when the file download is complete.
Is there a way to download the file and make modifications to the file's byte array on the fly in Silverlight.
I can not use Sockets, I can only download files from a Webserver.
Any help from you Gurus are appreciated
I can't see what is the problem with the Webclient.
You should copy the results of the WebClients in byte arrays.
Once every file has been downloaded, you can merge those byte arrays.
byte[] bytes;
public void DownloadFile()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += (s, e) =>
{
Stream stream = e.Result;
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
bytes = ms.ToArray();
};
webClient.OpenReadAsync(new Uri("http://myurl.com/file.zip"), UriKind.Absolute);
}
I'm trying to download a file from the web and save it locally, but I get an exception:
C# The process cannot access the file
'blah' because it is being used by
another process.
This is my code:
File.Create("data.csv"); // create the file
request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
request.Timeout = 30000;
response = (HttpWebResponse)request.GetResponse();
using (Stream file = File.OpenWrite("data.csv"), // <-- Exception here
input = response.GetResponseStream())
{
// Save the file using Jon Skeet's CopyStream method
CopyStream(input, file);
}
I've seen numerous other questions with the same exception, but none of them seem to apply here. Any help?
Update:
Thanks for the answers! Removing the File.Create(...) fixed it!
One comment on the documentation of OpenWrite: it is a little misleading, the brief description says:
Opens an existing file for writing.
The detailed description says:
If the file exists, it is opened for
writing at the beginning. The existing
file is not truncated.
Update 2.0:
It looks like the discrepancy is between IntelliSense/F1 and the online documentation. I thought it should be the same since I allow F1 to connect to the web when it's displaying documentation.
File.Create returns a FileStream - which you're not closing. That means you won't be able to open another stream writing to the same file until the finalizer has closed the existing stream.
Just get rid of the call to File.Create - File.OpenWrite will create it anyway. Alternatively, keep the FileStream around to write to:
using (Stream file = File.Create("data.csv"))
{
request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
request.Timeout = 30000;
using (var response = (HttpWebResponse)request.GetResponse())
using (Stream input = response.GetResponseStream())
{
// Save the file using Jon Skeet's CopyStream method
CopyStream(input, file);
}
}
Note that I'm also disposing of the WebResponse here, which you should do to make sure the connection is freed to the connection pool.
looks like File.Create returns an open FileStream object
http://msdn.microsoft.com/en-us/library/aa328775(v=VS.71).aspx
try
using (FileStream fs = File.Create("data.csv"))
and leave off the first File.Create
The problem is that the File.Create method actually opens the file and returns a FileStream object. It won't close the file until the object is garbage collected (which will happen at an indeterminate time). The FileStream object still gets created, regardless of whether or not you use it. Try this:
using (Stream file = File.Create("data.csv"),
input = response.GetResponseStream())
{
// Save the file using Jon Skeet's CopyStream method
CopyStream(input, file);
}
First. File.Create will return a stream that you should use for accessing the file.
Second, if that doesn't resolve the issue, if you google who lock you will find a program that let's you determine what process is accessing a file at the same time.