I'm working on a server/client application, in wich the client has the ability to upload/download a file, wich will be stored in SQL server (2012) as varbinay(max). The problem is that I want that the file will be uploaded directly to the database without saving the file on the server's hard drive, using ReadAllBytes method, wich only accepts a path parameter. Here is a fragment of the code used in the server side :
我正在研究服务器/客户端应用程序,客户端能够上传/下载文件,它将作为varbinay(max)存储在SQL server(2012)中。问题是我希望文件将直接上传到数据库,而不使用ReadAllBytes方法将文件保存在服务器的硬盘上,只接受路径参数。以下是服务器端使用的代码片段:
HttpPostedFile file1 = context.Request.Files[0];
byte[] buffer = new byte[file1.ContentLength];
file1.InputStream.Read(buffer, 0, file1.ContentLength);
Here is the code user in order to write into a file from data in the database :
以下是代码用户,以便从数据库中的数据写入文件:
foreach (var file in list)
{
System.IO.File.WriteAllBytes(path + file.FileName, file.FileContents);
}
1 个解决方案
#1
1
The HttpPostedFile
comes with an input stream you can read from. Read it into a byte array, then store it in the database. Or if your database interface (in the case of EF this is unlikely) directly accepts a stream, just put it in as-is,
HttpPostedFile附带了一个可以读取的输入流。将其读入字节数组,然后将其存储在数据库中。或者,如果您的数据库接口(在EF不太可能的情况下)直接接受流,只需将其按原样放入,
Do note that ASP.NET (or was it IIS?) may store a file upload temporarily on disk anyways, if it's very large.
请注意,ASP.NET(或它是IIS?)可能会暂时将文件上载存储在磁盘上,如果它非常大的话。
#1
1
The HttpPostedFile
comes with an input stream you can read from. Read it into a byte array, then store it in the database. Or if your database interface (in the case of EF this is unlikely) directly accepts a stream, just put it in as-is,
HttpPostedFile附带了一个可以读取的输入流。将其读入字节数组,然后将其存储在数据库中。或者,如果您的数据库接口(在EF不太可能的情况下)直接接受流,只需将其按原样放入,
Do note that ASP.NET (or was it IIS?) may store a file upload temporarily on disk anyways, if it's very large.
请注意,ASP.NET(或它是IIS?)可能会暂时将文件上载存储在磁盘上,如果它非常大的话。