1

我正在尝试上传文件并将其二进制内容保存到我的数据库字段中。每次我尝试保存时,它都会以0x.

以下是相关代码:

<form id="frmDefault" enctype="multipart/form-data" runat="server">
    <asp:FileUpload ID="fileInput" runat="server" style="margin: 8px 0 13px 0;" /><br />
    <asp:textbox rows="5" TextMode="multiline" runat="server" id="description" /><br />
    <asp:Button ID="btnUpload" Text="Upload File" runat="server" 
        onclick="btnUpload_Click" />
</form>

我建立变量:

HttpPostedFile file = fileInput.PostedFile;

而且,它达到了这个功能:

LoadFile(gAttachmentContentID, file.InputStream, trn);

其定义为:

public static void LoadFile(Guid gAttachmentContentID, Stream stm, IDbTransaction trn)
{
            const int BUFFER_LENGTH = 40 * 1024;
            byte[] binFILE_POINTER = new byte[32];
            //Testing check out check in
            // 01/20/2006 Paul.  Must include in transaction
            SqlProcs.spATTACHMENTS_CONTENT_InitPointer(gAttachmentContentID, ref binFILE_POINTER, trn);

            using (BinaryReader reader = new BinaryReader(stm))
            {
                int nFILE_OFFSET = 0;
                byte[] binBYTES = reader.ReadBytes(BUFFER_LENGTH);

                while (binBYTES.Length > 0)
                {
                    // 08/14/2005 Paul.  gID is used by Oracle, binFILE_POINTER is used by SQL Server. 
                    // 01/20/2006 Paul.  Must include in transaction
                    SqlProcs.spATTACHMENTS_CONTENT_WriteOffset(gAttachmentContentID, binFILE_POINTER, nFILE_OFFSET, binBYTES, trn);
                    nFILE_OFFSET += binBYTES.Length;
                    binBYTES = reader.ReadBytes(BUFFER_LENGTH);
                }
            }
}

如何读取正确的内容以便将整个文件保存到数据库?

谢谢你。

编辑:

    #region spATTACHMENTS_CONTENT_WriteOffset
    /// <summary>
    /// spATTACHMENTS_CONTENT_WriteOffset
    /// </summary>
    public static void spATTACHMENTS_CONTENT_WriteOffset(Guid gID, byte[] binFILE_POINTER, Int32 nFILE_OFFSET, byte[] byBYTES)
    {
        DbProviderFactory dbf = DbProviderFactories.GetFactory();
        using ( IDbConnection con = dbf.CreateConnection() )
        {
            con.Open();
            using ( IDbTransaction trn = con.BeginTransaction() )
            {
                try
                {
                    using ( IDbCommand cmd = con.CreateCommand() )
                    {
                        cmd.Transaction = trn;
                        cmd.CommandType = CommandType.StoredProcedure;
                        if ( Sql.IsOracle(cmd) )
                            cmd.CommandText = "spATTACHMENTS_CONTENT_WriteOff";
                        else
                            cmd.CommandText = "spATTACHMENTS_CONTENT_WriteOffset";
                        IDbDataParameter parID               = Sql.AddParameter(cmd, "@ID"              , gID                );
                        IDbDataParameter parFILE_POINTER     = Sql.AddParameter(cmd, "@FILE_POINTER"    , binFILE_POINTER    );
                        IDbDataParameter parMODIFIED_USER_ID = Sql.AddParameter(cmd, "@MODIFIED_USER_ID",  Security.USER_ID  );
                        IDbDataParameter parFILE_OFFSET      = Sql.AddParameter(cmd, "@FILE_OFFSET"     , nFILE_OFFSET       );
                        IDbDataParameter parBYTES            = Sql.AddParameter(cmd, "@BYTES"           , byBYTES            );
                        cmd.ExecuteNonQuery();
                    }
                    trn.Commit();
                }
                catch(Exception ex)
                {
                    trn.Rollback();
                    throw(new Exception(ex.Message, ex.InnerException));
                }
            }
        }
    }
    #endregion
4

1 回答 1

0

我遇到了同样的问题,即第一次将图像正确保存在数据库端,但如果随后验证失败,然后我在输入有效数据后尝试再次保存图像,我将进入0x图像列。为了解决这个问题,我做了@Ann L.所说的:

byte[] photo = null;

if(model.Photo != null)
{
    var stream = model.Photo.InputStream;
    stream.Position = 0;

    using(BinaryReader br = new BinaryReader(model.Photo.InputStream))
    {
        photo = br.ReadBytes(model.Photo.ContentLength);
    }
}
于 2013-09-26T23:25:55.880 回答