1

I have an ASP .Net (3.5) website. I have the following code that uploads a file as a binary to a SQL Database:

Print("        
            protected void UploadButton_Click(object sender, EventArgs e)
            {

            //Get the posted file
            Stream fileDataStream = FileUpload.PostedFile.InputStream;

            //Get length of file
            int fileLength = FileUpload.PostedFile.ContentLength;

            //Create a byte array with file length
            byte[] fileData = new byte[fileLength];

            //Read the stream into the byte array
            fileDataStream.Read(fileData, 0, fileLength);

            //get the file type
            string fileType = FileUpload.PostedFile.ContentType;

            //Open Connection
            WebSysDataContext db = new WebSysDataContext(Contexts.WEBSYS_CONN());

            //Create New Record
            BinaryStore NewFile = new BinaryStore();

            NewFile.BinaryID = "1";
            NewFile.Type = fileType;
            NewFile.BinaryFile = fileData;

            //Save Record
            db.BinaryStores.InsertOnSubmit(NewFile);

            try
            {
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }");

The files that will be uploaded are PDFs, Can you please help me in writing the code to get the PDF out of the SQL database and display it in the browser. (I am able to get the binary file using a linq query but not sure how to process the bytes)

4

4 回答 4

1

So are you really just after how to serve a byte array in ASP.NET? It sounds like the database part is irrelevant, given that you've said you are able to get the binary file with a LINQ query.

If so, look at HttpResponse.BinaryWrite. You should also set the content type of the response appropriately, e.g. application/pdf.

于 2008-10-27T15:12:10.530 回答
0

protected void Test_Click(object sender, EventArgs e) {

        WebSysDataContext db = new WebSysDataContext(Contexts.WEBSYS_CONN());

        var GetFile = from x in db.BinaryStores
                      where x.BinaryID == "1"
                      select x.BinaryFile;

        FileStream MyFileStream;
        long FileSize;

        MyFileStream = new FileStream(GetFile, FileMode.Open);
        FileSize = MyFileStream.Length;

        byte[] Buffer = new byte[(int)FileSize];
        MyFileStream.Read(Buffer, 0, (int)FileSize);
        MyFileStream.Close();

        Response.Write("<b>File Contents: </b>");
        Response.BinaryWrite(Buffer);

    }

我试过了,但这没有用。我在这一行“MyFileStream = new FileStream(GetFile, FileMode.Open);”上得到一个编译错误 我不确定我哪里出错了,是因为我存储它的方式吗?

于 2008-10-27T16:10:49.100 回答
0

How big are the files? Huge buffers (i.e. byte[fileLength]) are usually a bad idea.

Personally, I'd look at things like this and this, which show reading/writing data as streams (the second shows pushing the stream as an http response). But updated to use varchar(max) ;-p

于 2008-10-27T15:14:45.517 回答
0

当您在 SQL Server 中存储二进制文件时,它会将 OLE 标头添加到二进制数据中。因此,在将 byte[] 实际读入文件之前,您必须剥离该标头。这是你如何做到这一点的。

// First Strip-Out the OLE header
const int OleHeaderLength = 78;

int strippedDataLength = datarow["Field"].Length - OleHeaderLength;

byte[] strippedData = new byte[strippedDataLength];

Array.Copy(datarow["Field"], OleHeaderLength, 
    strippedData , 0, strippedDataLength );

运行此代码后,strippedData 将包含实际的文件数据。然后,您可以使用 MemoryStream 或 FileStream 对 byte[] 执行 I/O。

于 2009-04-14T13:24:38.730 回答