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

では、ASP.NET でバイト配列を提供する方法を本当に理解していますか? LINQクエリでバイナリファイルを取得できると言ったことを考えると、データベース部分は無関係のようです。

もしそうなら、HttpResponse.BinaryWriteを見てください。application/pdf など、応答のコンテンツ タイプも適切に設定する必要があります。

于 2008-10-27T15:12:10.530 に答える
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 に答える
0

protected void Test_Click(オブジェクト送信者, 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

ファイルの大きさは?巨大なバッファ (つまり、byte[fileLength]) は、通常は悪い考えです。

個人的には、データの読み取り/書き込みをストリームとして示すthisおよびthisのようなものを見たいと思います(2 つ目は、ストリームを HTTP 応答としてプッシュすることを示しています)。しかし、varchar(max) ;-p を使用するように更新されました

于 2008-10-27T15:14:45.517 に答える