将上载文件存储为SQL Server数据库中的二进制数据

时间:2015-11-27 20:01:18

标签: sql-server asp.net-mvc entity-framework

我一直在努力解决这个问题。我试图将文件上传到服务器并将其作为二进制数据保存在数据库中。

我知道如何将文件上传到服务器并且工作正常。

我需要知道如何将文件保存为数据库中的二进制数据。

这就是我所取得的成就:

在SQL Server数据库中,我有一个名为files的表,其中包含以下列:

Id, int, identity column 
FileName, nvarchar(50) 
UploadDate, datetime
FileContent, varbinary(max)

HomeController.cs

[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files)
{
    try
    {
        foreach (var file in files)
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);

            // extract the file content to byte array
            var content = new byte[file.ContentLength];
            // reads the content from stream
            file.InputStream.Read(content, 0, file.ContentLength);

            //get file extesion
            var fileExtension = Path.GetExtension(fileName);
            //save file name as uniqe
            var uniqueFileName = Guid.NewGuid().ToString();

            Files fu = new Files
            {
                FileName = uniqueFileName,
                UploadDate = DateTime.Now,
                FileContent = content
            };

            DB.Files.Add(fu);
            DB.SaveChanges();
        }
    }
    catch (Exception e) { }

    // redirect back to the index action to show the form once again
    return RedirectToAction("Index");
}

index.cshtml

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files" multiple/>
    <input type="submit" value="OK" />
}

此代码将记录插入数据库表中,记录如下所示:

Id  FileName                                UploadDate              FileContent
1   716d30a5-8019-4ec9-a5e9-5b3966296bfc    2015-11-27 21:50:27.037 <Binary data>

我不知道fileContent列填充文字<Binary data>的原因。

1 个答案:

答案 0 :(得分:0)

由于文件内容数据类型是二进制文件,因此Sql server在内部将其作为二进制数据类型进行管理。通过查看表格,您无法直接看到二进制文件。

如果您想在SSMS中查看二进制数据,请尝试this

这是检索binday数据的示例

// Assumes that connection is a valid SqlConnection object.
SqlCommand command = new SqlCommand(
  "SELECT pub_id, logo FROM pub_info", connection);

// Writes the BLOB to a file (*.bmp).
FileStream stream;                          
// Streams the BLOB to the FileStream object.
BinaryWriter writer;                        

// Size of the BLOB buffer.
int bufferSize = 100;                   
// The BLOB byte[] buffer to be filled by GetBytes.
byte[] outByte = new byte[bufferSize];  
// The bytes returned from GetBytes.
long retval;                            
// The starting position in the BLOB output.
long startIndex = 0;                    

// The publisher id to use in the file name.
string pubID = "";                     

// Open the connection and read data into the DataReader.
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

while (reader.Read())
{
  // Get the publisher id, which must occur before getting the logo.
  pubID = reader.GetString(0);  

  // Create a file to hold the output.
  stream = new FileStream(
    "logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
  writer = new BinaryWriter(stream);

  // Reset the starting byte for the new BLOB.
  startIndex = 0;

  // Read bytes into outByte[] and retain the number of bytes returned.
  retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);

  // Continue while there are bytes beyond the size of the buffer.
  while (retval == bufferSize)
  {
    writer.Write(outByte);
    writer.Flush();

    // Reposition start index to end of last buffer and fill buffer.
    startIndex += bufferSize;
    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
  }

  // Write the remaining buffer.
  writer.Write(outByte, 0, (int)retval - 1);
  writer.Flush();

  // Close the output file.
  writer.Close();
  stream.Close();
}

// Close the reader and the connection.
reader.Close();
connection.Close();

来源:MSDN