1

I'm trying to upload a file. However, much of the material I find on the Internet explains how to save the file to a folder on the server, but for my solution I just needed to buffer. Is it possible? I upload the file to the server buffer, then read and clean.

Obs.: I am using telerik components, and need to read / import an Excel file.

Thanks

2
  • i dont think so putting in buffer is a good option Commented Feb 2, 2011 at 16:36
  • They are small files, and throws them into a folder I see that is not the best solution. But, what is your opinion? @anishMarokey Commented Feb 2, 2011 at 16:39

2 Answers 2

2

So if you use a binary stream, the approach to buffer it regardless of the context of execution (asp.net vs winforms or whatever) is pretty common:

public static byte[] ReadFile(string filePath)
{
  byte[] buffer;
  FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  try
  {
     int length = (int)fileStream.Length;  // get file length
     buffer = new byte[length];            // create buffer
     int count;                            // actual number of bytes read
     int sum = 0;                          // total number of bytes read

    // read until Read method returns 0 (end of the stream has been reached)
    while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
       sum += count;  // sum is a buffer offset for next reading
    }
  finally
  {
    fileStream.Close();
  }
   return buffer;
 }

Also, check out this:

http://www.yoda.arachsys.com/csharp/readbinary.html

Sign up to request clarification or add additional context in comments.

Comments

0

The default FileUpload component presents you the file as a Byte[] - you could just wrap it in a MemoryStream:

var strm = new MemoryStream(fileUpload.PostedFile.Contents);
  // I think its PostedFile.Contents, something like that anyway

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.