Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I get a file input stream from its path?

Tags:

c#

I have the path of a file I would like to upload but the method takes the file input Stream.

Can anyone tell me how I can get the input stream from the path?

I have upload files before when using a open file dialog and that takes the file in as a file input Stream, but the new section of my webpage the user does not select the file, I have to go grab it with code, but I know its file path.

public string uploadfile(string token, string filenameP, DateTime modDate, HttpPostedFileBase file)
{
    //... code to upload file
}

I would like something like the ClassLoader like in java.

See this question here.

https://stackoverflow.com/a/793216/1479146

like image 850
Pomster Avatar asked Oct 01 '12 10:10

Pomster


2 Answers

In C#, the way to get a Stream (be it for input or for output): use a derived class from Stream, e.g. new FileStream(inputPath, FileMode.Open)

like image 74
Wolfgang Grinfeld Avatar answered Sep 19 '22 05:09

Wolfgang Grinfeld


You can use StreamWrite or StreamReader class for this purpose:

// for reading the file
using (StreamReader sr = new StreamReader(filenameP)) 
{
    //...
}

// for writing the file
using (StreamWriter sw = new StreamWriter(filenameP)) 
{
    //...
}

Reference: http://msdn.microsoft.com/en-us/library/f2ke0fzy.aspx

like image 32
Furqan Safdar Avatar answered Sep 18 '22 05:09

Furqan Safdar