1

So I have this code:

if ((uplImage.FileName != ""))
{
    byte[] raw = new byte[10000];

    //to allow only jpg gif and png files to be uploaded.
    string extension = Path.GetExtension(uplImage.PostedFile.FileName.ToUpper());
    if (((extension == ".JPG") || ((extension == ".GIF") || (extension == ".PNG"))))
    {

        DALBio bio = new DALBio();

        FileStream fs = new FileStream(uplImage.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
        fs.Read(raw, 0, System.Convert.ToInt32(fs.Length));

        bio.PlayerID = Session["playerID"].ToString();
        bio.Pending = 'Y';
        bio.Photo = raw;
        DALBio.insertImage(bio);
    }
}

When I try this, the stream is not reading the image. raw never gets the image. It stays empty and it gets caught when it executes the stored procedure, saying I never passed the image. I am confident that code is just fine. I do not know why I can't get the image into my byte array.

5
  • The code seems to be correct. Are you sure 10000 bytes is enough? I tried it and it works fine. Had to boost the bytes up to 400 000 for my small test picture. May 15, 2013 at 16:58
  • Consider storing a filename reference to the file in the db, and store the actual file elsewhere.
    – Mike Cole
    May 15, 2013 at 17:13
  • my image is actually just 60 bytes big, when i tried to just say byte [] raw and then a few liens later use raw, it just skipped over raw. so thats why i gave it a size and instantiated it earlier
    – dwarf
    May 15, 2013 at 17:16
  • Also a similar question that might help stackoverflow.com/questions/8880213/…
    – Amitd
    May 15, 2013 at 17:17
  • Any reason you can't use var raw = System.File.IO.ReadAllBytes(uplImage.PostedFile.FileName) ?
    – hometoast
    May 15, 2013 at 17:39

2 Answers 2

0

You can create/define the raw array as

     FileStream fs = new FileStream(uplImage.PostedFile.FileName, 
                         FileMode.OpenOrCreate, FileAccess.ReadWrite, 
                          FileShare.Read);

    raw = new byte[fs.Length]; 

// same code as above..

You can also try similar code

        System.IO.Stream myStream;
        Int32 fileLen;

        // Get the length of the file.
        fileLen = uplImage.PostedFile.ContentLength;  


        // Create a byte array to hold the contents of the file.
        Byte[] input = new Byte[fileLen];

        // Initialize the stream to read the uploaded file.
        myStream = uplImage.FileContent;

        // Read the file into the byte array.
        myStream.Read(input, 0, fileLen); 
        // input will hold the byte array
2
  • forwhatever reason, using this example works. not i just have to convert from varchar to varbinary as my stored proc doesnt like the implicit conversion
    – dwarf
    May 15, 2013 at 17:47
  • Code is from msdn msdn.microsoft.com/en-us/library/…
    – Amitd
    May 15, 2013 at 17:51
0

What I do is I get the ByteArray:

public Byte[] GetArrayFromFile(string path)
{
  Byte[] data = null;

  FileInfo fileInf = new FileInfo(path);
  long numBytes = fileInf.Length;

  using (FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read))
  {
    BinaryReader bReader = new BinaryReader(fStream);

    data = bReader.ReadBytes((int)numBytes);
  }
  return data;
}

Then it stores in the database correctly using Entity Framework (it should be working if your DAL inserts your objects correctly in the database).

2
  • 2
    Also note FileStream is IDisposable, so you might want to dispose of it or put it in a using block. May 15, 2013 at 16:54
  • this still gives me the same problem. using the code in the answer provided, data stays at a size of 0, nothing gets written to it.
    – dwarf
    May 15, 2013 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.