FileStream or WebClient

1.8k views Asked by At

I'm currently generating some preview with the Wopi and the Office Web App technologies from Microsoft, and i'm having an issue. Sorry for my bad english, but i'll try to express myself as well as possible.

So ! I'm generating the preview by downloading a file's content and storing it into an HttpContent. First, i tried this on files that were in the ~/App_Data/ of my project. I read it's content with the FileStream class and transformed it into HttpContent byusing this : StreamContent(myFileStream). So, THIS is working perfectly !

BUT, i need my project to deal with files that are stored on servers only reachable from the internet (So physical urls should look like this : http://myServer/res/file.pdf for example). I couldn't use the FileStream class here, so i did it like this :

byte[] tmp;
using (WebClient client = new WebClient())
{
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    tmp = client.DownloadData("http://myServer/res/file.pdf");
}
myHttpContent = new ByteArrayContent(tmp);

The thing is that this little sample seems to be working, but the preview isn't generated after that, and it is the only piece of code that i changed. I checked some stuff befor posting here, so : i do have access to the file and i am allowed to read it.

So, my question is, it this a good way to get the files content, is it outdated, what should i do to try to patch this ?

I hope my post is clear enough and thanks for reading !

1

There are 1 answers

0
Daniel Slater On BEST ANSWER

I think you can do the following

MemoryStream ms;
using (WebClient client = new WebClient())
{
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    ms = new MemoryStream(client.DownloadData("http://myServer/res/file.pdf"));
}

Then use the memory stream should behave identically to your filestream.