12

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

peterh
  • 11,875
  • 18
  • 85
  • 108
Pomster
  • 14,567
  • 55
  • 128
  • 204

5 Answers5

12

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

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • Will this give me a file inputStream? – Pomster Oct 01 '12 at 11:03
  • This will give you the stream of the file and it will definitely work for you. – Furqan Safdar Oct 01 '12 at 11:05
  • @Pommy, what is file InputStream and what do you really want to do with it. Please explain. – Furqan Safdar Oct 01 '12 at 11:12
  • When you use a file.open.dialog it saves the fileinputStream {System.Web.HttpInputStream} and i pass that into my upload method as the parameter file witch is a HttpPostedFileBase – Pomster Oct 01 '12 at 11:16
  • So again you can have a stream from it, i.e. Stream stream = file.InputStream; I am clueless what do you really want to do? – Furqan Safdar Oct 01 '12 at 11:34
  • the inputStream parameter of HttpPostedFileBase you need to set when you are downloading not while uploading, just set the fileName property to the fully qualified name of your file and try – Mukul Goel Oct 01 '12 at 11:37
8

Use File.OpenRead(path) FileStream Class on Microsoft Docs

divay pandey
  • 152
  • 4
  • 8
3

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)

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

P.S. Don't forget to include System.IO namespace

Edit: the stream manipulating classes in System.IO are wrapping Stream base class. reader.BaseStream property will give you that stream.

george.zakaryan
  • 960
  • 1
  • 6
  • 18
  • The file input stream is coming in as the last parameter in the uploadfile method. So i need the input stream to pass into the method. How can StreamReader give me the file inputStream. – Pomster Oct 01 '12 at 11:08
0

First open an input stream on your file

then pass that input stream to your upload method

eg: to open stream on 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();
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • What do you mean by open input stream? – Pomster Oct 01 '12 at 11:04
  • 1
    I don't see how the StreamReader or Writer can give me the file inputStream? I need the input stream for the zip file i am trying to upload. The input Stream will be passed though as the last parameter. – Pomster Oct 01 '12 at 11:10
  • I mean, create a input stream ( StreamReader) on your file. basically to upload your file, you cant just send your file as parameter, you need to send stream of bits to upload data.. for this , first you open a StreamReader, a StreamReader class opens an inputStream on the file , reads the file bit by bit. So now you have a stream of bits for your file. Now you can send this stream of bits to the upload method which will in turn send this stream to the server – Mukul Goel Oct 01 '12 at 11:11
  • in your code , file is ojbect of HttpPostedFileBase class, you need to set the inputStream when you are reading from an uploaded file(i.e. while downloading a file) to upload just set file.FileName property and it will do the trick – Mukul Goel Oct 01 '12 at 11:16