views:

59

answers:

1

Hi, I'm trying to get my website to allow users to upload various files (HttpPostedFile), which are then stored in an Oracle database as BLOBs. Here's what I've got so far:

    public static bool insertFile(int pid, HttpPostedFile file, string filedesc)
    {
        string filename = file.FileName.Remove(0, file.FileName.LastIndexOf("\\") + 1);
        byte[] filebytearray = new byte[file.ContentLength];
        BinaryReader br = new BinaryReader(file.InputStream);
        filebytearray = br.ReadBytes(file.ContentLength);

        if (filedesc == string.Empty)
        {
            filedesc = "No description.";
        }
        OracleConnection conn = new OracleConnection(connectionString);
        OracleCommand cmd = new OracleCommand("database", conn);
        cmd.BindByName = true;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new OracleParameter("pfiledata", OracleDbType.Blob)).Value = filebytearray;
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Errors.WriteToEventLog("File insert", ex);
            return false;
        }
        finally
        {
            conn.Dispose();
            cmd.Dispose();
            file.InputStream.Dispose();
        }
    }

The file successfully gets uploaded and downloaded - however, the downloaded file is not the same as the uploaded file. I've already verified that the contents are the same as the file it goes into and out of the database, meaning that the file is either not being converted correctly, or it is not being saved correctly by the client. The two files are identical in size on the disk, but not according to Windows. It appears that the downloaded copy of the file is missing 3 bytes in the very beginning of the file, according to hex editor.

Here is what I'm using to transfer the file to the client: Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + fileinfo[1]);
Response.AddHeader("Content-Length", filedata.Length.ToString());
Response.ContentType = "application/octet-stream"; Response.BinaryWrite(filedata);

Any help would be appreciated.

A: 

A lot of the sample code I am seeing online states

br.BaseStream.Position = 0;

before reading; I don't see why, but is it possible that you need to explicitly set the start position?

pjabbott
Thanks, that worked. It seems rather odd to me that BinaryReader wouldn't capture the stream with index 0 as the default, but now that it works I'm not going to question it too much.
Kay Ell