-1

I need to create a file stream directly at a server location using an URL. Something like

fs = new FileStream("http://SiteName.in/abc/xyz/pqr.pdf", FileMode.Create);

but its giving me this exception:

URI formats are not supported

I tried it with many other options but not providing satisfactory result.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Vrishali
  • 42
  • 1
  • 5
  • Do you want to read from or write to the stream? – H H Feb 27 '15 at 10:29
  • I think they want to upload a file to a server, given the FileMode.Create – Dasanko Feb 27 '15 at 10:30
  • possible duplicate of [URL Formats are not supported](http://stackoverflow.com/questions/17918433/url-formats-are-not-supported) – stefankmitph Feb 27 '15 at 10:31
  • i need to create and write directly at specified location. when we work with fs = new FileStream("c:\\pqr.pdf", FileMode.Create) it works but doesn't work by providing URL m serching correct way to do so.. – Vrishali Feb 27 '15 at 10:36

2 Answers2

3

You can't create a file stream of a HTTP request. That is not how the class and the web works.

Use WebClient.OpenWrite or the direct WebClient.UploadString instead:

WebClient wc = new WebClient();
using (Stream s = wc.OpenWrite("url"))
{
    ...
}

Of course, your server should support the POST request though. You can also use the more manual HttpWebRequest class.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

You can simply download your file into Stream

public static Stream DownloadFile(string TargetFile)
{
    WebClient MyWebClient = new WebClient();
    byte[] BytesFile = MyWebClient.DownloadData(TargetFile);

    MemoryStream iStream = new MemoryStream(BytesFile);

    return iStream;
}
Mehdi Daustany
  • 1,018
  • 4
  • 10
  • 23