0

我试图让我的网站允许用户上传各种文件(HttpPostedFile),然后将这些文件作为 BLOB 存储在 Oracle 数据库中。这是我到目前为止所得到的:

    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();
        }
    }

文件成功上传和下载 - 但是,下载的文件与上传的文件不同。我已经验证了内容与它进出数据库的文件相同,这意味着该文件要么没有被正确转换,要么没有被客户端正确保存。这两个文件在磁盘上的大小相同,但根据 Windows 不同。根据十六进制编辑器,下载的文件副本似乎在文件的开头缺少 3 个字节。

这是我用来将文件传输到客户端的内容: Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + fileinfo[1]);
Response.AddHeader("内容长度", filedata.Length.ToString());
Response.ContentType = "应用程序/八位字节流"; Response.BinaryWrite(文件数据);

任何帮助,将不胜感激。

4

1 回答 1

0

我看到在线状态的很多示例代码

br.BaseStream.Position = 0;

读之前; 我不明白为什么,但是您是否可能需要明确设置起始位置?

于 2009-07-27T23:17:07.233 回答