Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Image to Filestream

I am loading an image using

OpenFileDialog open = new OpenFileDialog();

After I select the file, "open" is populated with several items, including the path.

Now I would like to load the file into a filestream (or something similar) to be sent via a webservice... is this possible?

thanks

like image 516
mouthpiec Avatar asked Jul 24 '10 16:07

mouthpiec


3 Answers

You can open the file with FileStream:

FileStream file = new FileStream("path to file", FileMode.Open);

You can then pass this through to the web service http context Response.OutputStream property. You will still need to set the correct mime type and various headers, but this works well:

HttpContext.Current.Response.OutputStream = file;

Having said that, the easiest way to send a file from a web service (or web app) is to use the Response.WriteFile method:

Response.WriteFile("Path To File");
like image 91
Oded Avatar answered Nov 16 '22 05:11

Oded


try this:

byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);
like image 38
Arash Avatar answered Nov 16 '22 06:11

Arash


Yes it is possible to create an image

var img = Image.FromFile(/*path*/);

or into a stream

var file = new FileStream("path to file", FileMode.Open);

But hot it should be send it is up to You to decide

sendToWs(img)

like image 31
Damian Leszczyński - Vash Avatar answered Nov 16 '22 06:11

Damian Leszczyński - Vash