How can I get the file input stream from its path?

I have a file path that I would like to download, but the method accepts the input stream.

Can someone tell me how can I get input from a path?

I have files to download before using the open file dialog, which takes the file as input data stream, but in the new section of my web page the user does not select the file, I need to capture it using the code, but I know its path to file.

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

I would like something like ClassLoader, as in java.

See this question here.

stack overflow

+4
source share
4 answers

For this purpose, you can use the StreamWrite or StreamReader class:

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

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

+5
source
 public string uploadfile(string token, string filenameP, DateTime modDate, HttpPostedFileBase file) { using (StreamReader reader = new StreamReader(filenameP)) { //read from file here } } 

PS Remember to include the System.IO namespace

Edit: The threads that manage classes in System.IO wrap the base class of Stream. reader.BaseStream will give you this stream.

+1
source

First open the input stream in your file

then pass this input stream to your download method

for example: open a stream in files

 // Character stream writing StreamWriter writer = File.CreateText("c:\\myfile.txt"); writer.WriteLine("Out to file."); writer.Close(); // Character stream reading StreamReader reader = File.OpenText("c:\\myfile.txt"); string line = reader.ReadLine(); while (line != null) { Console.WriteLine(line); line = reader.ReadLine(); } reader.Close(); 
0
source

In C #, a way to get a Stream (whether for input or for output): use a derived class from Stream, for example. new FileStream (inputPath, FileMode.Open)

0
source

Source: https://habr.com/ru/post/1437155/


All Articles